HELP : Summing amounts along a Hierarchy.

Hi All,
I need a query to help me with summing-up amounts along a hierarchy.
Data is only at the lowest level and needs to be summed up along the
hierarchy accordingly. Each level above the lowest level needs to
have an amount that is the sum of amounts in the levels below.
Sample Data:
Id Parent_id Level amount
1 1 0
2 1 2 0
3 1 2 0
4 1 2 0
5 2 3 0
6 2 3 0
7 3 3 0
8 4 3 0
9 5 4 10
10 5 4 10
11 6 4 10
12 8 4 10
13 8 4 10
Desired Output:
Id Amount
1 50
2 30
5 20
9 10
10 10
6 10
11 10
3 0
7 0
4 20
8 20
12 10
13 10
I have tried using the following select but the results
select tn.parent_id,
sum (tn.amount)
from table_name tn
group by tn.parent_id
connect by prior tn.id = tn.parent_id
start with tn.task_id = 1
and the results I get are:
Id Amount
1 0
2 0
5 20
9 10
10 10
6 10
11 10
3 0
7 0
4 0
8 20
12 10
13 10
Am I on the right track and what do I need to modify if so.
Otherwise, will you please suggest a new way to do this ?
Thanks in advance,

Like this one ?
SQL> with tbl as
  2  (select 1  c1, null c2, 0 c3, null c4 from dual union all
  3   select 2  c1, 1 c2, 2 c3, 0  c4 from dual union all
  4   select 3  c1, 1 c2, 2 c3, 0  c4 from dual union all
  5   select 4  c1, 1 c2, 2 c3, 0  c4 from dual union all
  6   select 5  c1, 2 c2, 3 c3, 0  c4 from dual union all
  7   select 6  c1, 2 c2, 3 c3, 0  c4 from dual union all
  8   select 7  c1, 3 c2, 3 c3, 0  c4 from dual union all
  9   select 8  c1, 4 c2, 3 c3, 0  c4 from dual union all
10   select 9  c1, 5 c2, 4 c3, 10 c4 from dual union all
11   select 10 c1, 5 c2, 4 c3, 10 c4 from dual union all
12   select 11 c1, 6 c2, 4 c3, 10 c4 from dual union all
13   select 12 c1, 8 c2, 4 c3, 10 c4 from dual union all
14   select 13 c1, 8 c2, 4 c3, 10 c4 from dual )
15  select c1, c2, c3, c4
16  from   (select c1, c2, c3, sum(c4) over (partition by connect_by_root c1) as c4, level lv
17          from   tbl
18          connect by prior c1 = c2)
19  where lv = 1;
             C1              C2              C3              C4
              1                               0              50
              2               1               2              30
              3               1               2               0
              4               1               2              20
              5               2               3              20
              6               2               3              10
              7               3               3               0
              8               4               3              20
              9               5               4              10
             10               5               4              10
             11               6               4              10
             12               8               4              10
             13               8               4              10
13 rows selected.
Elapsed: 00:00:00.03Nicolas.

Similar Messages

  • Summing up along a hierarchy

    Hi,
    I have a requirement... A simplistic explanation of it is...
    e.g.
    Product Grp1        Sales qty        Price        Total Sales
        Product A            1                200            200
        Product B            1                300            300
    Sales qty and Price are two separate accounts.  I don't have Total Sales as another account but am calculating it in Excel.
    Can I force the system to calculate first and the aggregate instead of aggregate and calculate
    Any way to calculate the Total Sales at Product Grp1 without using Script logic ?
    thanks

    Hi Ram,
    If you want to store the values of Total Sales, then you need to have this as a member in your dimension. If you want the calculation to happen in the excel, you can keep the formula in the excel sheet only and when you send the data, the data for total sales will also be sent to the database. Alternatively, you can keep a dimension formula for total sales in your dimension.
    Hope this helps.

  • Sum amount - Positive and negative

    How do I have a select statement that sum up all the negative and positive values into 2 serperate column? sth like this:
    SELECT sum(amount) as negative, sum(amount) as positive FROM account_table WHERE customer_id = 111;Here is how the output look like when I retrieve the amount of customer_id = 111:
    Amount
    1.4
    300
    -1.22
    -100The expected output that I want should be like this:
    Positive          Negative
    301.4                -101.22

    DECODE and SIGN functions can help
    SQL> select * from t
      2  /
           VAL
             1
            -2
             3
            -4
             5
            -6
             7
            -8
             9
           -10
    10 rows selected.
    SQL> select sum(decode(sign(val),1,val)) pos, sum(decode(sign(val),-1,val)) neg
      2    from t
      3  /
           POS        NEG
            25        -30

  • Sum amount within a date range

    I have 2 tables where I Need to sum up sales amount within a booking date range.
    Below an example to help illustrate this task.
    Table1:
    CustomerID BookingDate (YYYY-MM-DD)
    1 2014-04-29
    1 2014-07-30
    2 2014-03-31
    2 2014-06-30
    Table2:
    CustomerID SalesDate (YYYY-MM-DD) Amount
    1 2014-01-30 20
    1 2014-02-25 30
    1 2014-05-20 100
    1 2014-07-30 200
    1 2014-09-30 80
    2 2014-03-20 50
    Result:
    CustomerID BookingDate (YYYY-MM-DD) Sum(Amount From Table2)
    1 2014-04-29 50 -- SalesDate between 2014-01-01 and 2014-04-29
    1 2014-07-30 300 -- SalesDate between 2014-04-30 and 2014-07-30
    2 2014-03-31 50 -- SalesDate between 2014-01-01 and 2014-03-31
    2 2014-06-30 0 -- SalesDate between 2014-04-01 and 2014-06-30

    Please try this code:
    declare @Table1 table
    (CustomerID int,BookingDate date );
    insert @Table1
    values
    ( 1, '2014-04-29' ),
    (1, '2014-07-30' ),
    (2, '2014-03-31' ),
    (2, '2014-06-30') ;
    declare @Table2 table
    (CustomerID int, SalesDate date, Amount int);
    insert @Table2
    values
    (1,'2014-01-30',20) ,
    (1,'2014-02-25',30) ,
    (1,'2014-05-20',100) ,
    (1,'2014-07-30',200) ,
    (1,'2014-09-30',80) ,
    (2,'2014-03-20',50) ;
    with cte as
    select
    CustomerID,
    BookingDate ,
    row_number() over (partition by CustomerID order by BookingDate ) as rn
    from @Table1 )
    , cte2 as
    select
    T2.CustomerID ,
    isnull(T1.BookingDate, '2014-01-01') as FromDate,
    T2.BookingDate as ToDate
    from cte as T1
    right join cte as T2
    on T1.rn = T2.rn - 1
    and T1.CustomerID = T2.CustomerID
    select
    b.CustomerID ,
    b.ToDate as BookingDate,
    isnull(sum(a.Amount), 0) as [Sum(Amount From Table2)]
    from @Table2 as a
    right join cte2 as b
    on a.CustomerID = b.CustomerID
    and a.SalesDate > b.FromDate
    and a.SalesDate <= b.ToDate
    group by
    b.CustomerID ,
    b.ToDate;
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

  • Sum(amount) from 4 tables in sql

    Hi, Everyone,
    I have 4 tables which has pr_code,amount in each table.
    I need the sum(amount) for each pr_code from all the four tables?
    How could i write a sql to achieve this?
    Please help!
    Regards
    Su

    one option would be:
    select sum(cnt) sum_cnt
      from (select count(*) cnt from user_tables
             union all
            select count(*) cnt from user_objects
             union all
            select count(*) cnt from user_indexes
       SUM_CNT
           151

  • BRFplus: Sum Amount in Table

    Experts,
    We are having trouble summing amount columns of tables in BRFplus.  We have tried 2 different ideas and each has a different error:
    Table Operation - sum the column containing the amount.  This returns a currency error.  It is not possible to pass a currency at runtime because it does not exist in the table structure.
    TABLE_SUM_AMOUNT - create a formula and use this function to calculate the amount.  This always returns the error "Column 0050569BA4BE1EE487810606D083E1 does not exist in table Table Type."  Regardless of the column or the table type, this same error appears for all TABLE_SUM* functions.
    Any ideas?  Your help would be greatly appreciated.

    My specific requirement is to sum the BETRW amount field on an internal table of structure DFKKOP.  In ABAP, the structure contains a currency field called WAERS.  If you attempt to create a table Data Object in BRFplus based on the DFKKOP structure, for some reason the WAERS field disappears:
    This is really a very simple and common use case.  My function module that calls BRFplus is posted below.  What I need to know from the experts is:
    ****HOW DO I SUM THE FIELD BETRW IN TABLE DFKKOP?****
    The examples posted so far are not helping at all.
    FUNCTION ZFICA_BRFPLUS_SUM_TEST.
    *"*"Local Interface:
    *"  EXPORTING
    *"     VALUE(EV_AMOUNT) TYPE  BETRW_KK
    *"  TABLES
    *"      IT_DFKKOP STRUCTURE  DFKKOP
    *"  EXCEPTIONS
    *"      NO_INKGP_FOUND
       CONSTANTS:  lv_function_id TYPE if_fdt_types=>id VALUE '0050569BA4BE1EE48FE94B283920E9F1'. "THE BRFPLUS FUNCTION
       DATA: lt_name_value  TYPE           abap_parmbind_tab,
             ls_name_value  TYPE           abap_parmbind,
             lv_timestamp   TYPE           timestamp,
             exc            TYPE REF TO    cx_fdt,
             lr_data        TYPE REF TO    data.
       FIELD-SYMBOLS: <logmsg> TYPE if_fdt_actn_message_log=>s_log_msg,
                      <wa>     TYPE any.
       GET TIME STAMP FIELD lv_timestamp.
       "SEND THE OPEN ITEMS TO BRFPLUS
       CLEAR ls_name_value.
       MOVE: 'IT_DFKKOP' TO ls_name_value-name.
       GET REFERENCE OF IT_DFKKOP INTO lr_data.
       TRY.
           CALL METHOD cl_fdt_function_process=>move_data_to_data_object
             EXPORTING
               ir_data        = lr_data
               iv_function_id = lv_function_id
               iv_data_object = 'IT_DFKKOP'
               iv_timestamp   = lv_timestamp
             IMPORTING
               er_data        = ls_name_value-value.
         CATCH cx_fdt_input.
       ENDTRY.
       INSERT ls_name_value INTO TABLE lt_name_value.
       "THEN CALL THE BRFPLUS FUNCTION
       TRY.
           CALL METHOD cl_fdt_function_process=>process
             EXPORTING
               iv_function_id = lv_function_id
               iv_timestamp   = lv_timestamp
             CHANGING
               ct_name_value  = lt_name_value.
         CATCH cx_fdt_input cx_fdt INTO exc.
           MESSAGE exc TYPE 'E'.
       ENDTRY.
       "NOW GET THE RESULT FROM THE CALL TO THE BRFPLUS FUNCTION
       cl_fdt_function_process=>get_context_value( EXPORTING iv_function_id      = lv_function_id
                                                             iv_data_object      = 'EV_AMOUNT'
                                                             iv_timestamp        = lv_timestamp
                                                   IMPORTING ev_data             = EV_AMOUNT ).
    ENDFUNCTION.

  • SUM - amount of product in all WAREHOUSEs

    Hi
    I am sql beginner and have a problem with collecting data from Oracle DB.
    I have a table ODT with all the products (nr of product and nr of warehouse are the PK) (NTWR and NODD).
    for every warehouse the product has its row.
    for example product nr 658:
    NAME NTWR NODD AMOUNT
    EXAMPLEPRODUCT658 658 1 30
    EXAMPLEPRODUCT658 658 2 207
    EXAMPLEPRODUCT658 658 3 1602
    I'd like to learn how to sum the amount for NODD.
    Example output
    NAME NTWR AMOUNT
    EXAMPLEPRODUCT658 658 237 //the amount for NODD 1&2
    EXAMPLEPRODUCT768 768 568 //the amount for NODD 1&2
    I made a query like this to sum amount for a product by entered number:
    SELECT SUM(SUMAMO) FROM (SELECT ODT.AMOUNT SUMAMO, ODT.NTWR, ODT.NAME FROM ODT WHERE ODT.NTWR = '657' AND ODT.NODD IN (1,2))
    and it works for one product
    great! :) but i entered this query into larger one with product number and name:
    SELECT * FROM
    SELECT DISTINCT a.NTWR,
    (SELECT SUM(SUMAMO) FROM (SELECT ODT.AMOUNT SUMAMO, ODT.NTWR, ODT.NAME FROM ODT WHERE ODT.NTWR = a.NTWR AND ODT.NODD IN (1,2))),
    a.NAME
    FROM ODT a)
    and i have an error for a.NTWR in SUM -> bad identifier
    What can i do to count sum for every product?
    1. Sorry for my English
    2. I would appreciate any help

    Can you send me the exact query that you are using?
    But make sure that in group by clause, you are using the same arguments, as there are present in your select statement
    for eg
    incorrect one
    select a,b from tem
    where a=1
    and b=2
    group by b;
    Correctone
    select a,b from tem
    where a=1
    and b=2
    group by a,b;

  • How to do "for each month in 2008 sum amount where category is cash"

    The table format is:
    Date(MM/DD/YYYY) Amount Description Category Account
    01.02.2008 -21000 benzina (C) Calatorii Cash
    01.04.2008 -2000 Mancare (C) Calatorii Cash
    01.05.2008 -2000 Mancare (C) Comisioane Cash
    05.12.2008 4706 diverse (C) Comisioane BT - Mondo
    08/25/2008 8807 diverse (C) Calatorii BRD - PFA
    08/25/2008 104000 diverse (C) Comisioane BRD - Multiplan
    10.10.2008 6385849 diverse (C) Comisioane BRD - Atucont
    10/26/2008 943699 diverse (C) Calatorii BRD - ISIC
    10/27/2008 95313 diverse (C) Comisioane Cash
    10/28/2008 -300 Comisioane (C) Comisioane BRD - ISIC
    I am trying to do the following:
    for each month in 2008
    sum (Amount) where Category is (C) Calatorii
    =>
    January -23000
    February 0
    for each month in 2009 up until now
    sum (Amount) where Category is Cash
    =>
    January 35000
    With these 2 samples I can manage the other functions I need.
    Thanks in advance.

    Question asked and responded several times.
    In column H of the first table I inserted the formula:
    =YEAR(B)*100+MONTH(B)
    In column B of the second table, the formula is:
    =SUMIFS(Tableau 1 :: C,Tableau 1 :: G,"=Cash",Tableau 1 :: H,"="&A)
    CAUTION
    I don't know your system settings.
    If your system uses the decimal comma, you will have to use:
    =SUMIFS(Tableau 1 :: C;Tableau 1 :: G;"=Cash";Tableau 1 :: H;"="&A)
    Yvan KOENIG (from FRANCE jeudi 23 avril 2009 15:04:49)

  • How can I subtract in formcalc  sum(amount[0],amount[1],amount[2])

    How can I subtract amount[3] from this sum?  sum(amount[0],amount[1],amount[2])

    sum(amount[0], amount[1], amount[2]) - amount[3]
    Kyle

  • How to get sum amount (wrbtr) from table BSIS group by gjahr hkont monat

    Hello! I am new to this forum, and to ABAP. Hope my question is not obvious.
    I want to get sum amount from table BSIS, group by year, period an account. Normally I will try a SQL select like this:
    SELECT gjahr monat hkont sum( wrbtr ) as wrbtr INTO CORRESPONDING FIELDS OF TABLE itab FROM BSIS WHERE (itabcond) GROUP BY gjahr monat hkont .
    The problem is that the amount in field wrbtr is all positive. The result gets wrong. The debet/credit indicator shkzg determines what is positive/negative. How to solve this in the most efficient way? Should I read all accounting documents into an internal table, and loop through them, changing the sign, and then make some new select, or is there a better way?
    Regards Dag-Egil
    Message was edited by:
            Dag-Egil Bull Sletholt

    Here are the objects in the trace list:
    SKB1
    USR05
    TRDIR
    SKA1
    SKAT
    SKB1
    BSIS
    PRPS
    BSAS
    V_LTDX
    USR02
    RFBLG
    V_LTDX
    LTDX
    RTXTH
    RTXTF
    RTXTT
    TRDIR
    The most of this call to this tables returns 1-2 records, apart from BSIS, where it returns 10 records. This is the same as accounting documents in the transaction FBL3N. When i try this again with more records returned I still have no call to table GLT0.

  • Help needed - OBIEE - Level Based Hierarchy - Expanding Month for one year affects all previous years

    I am creating a level based hierarchy for the Date Dimension
    year --> quarter --> month --> date (one path)
    year .--> month --> date (the second path).
    I create the pivot table using these dim hierarchies. But the problem is,
    when I click on, say "Month" under year 2012, it expands everything above 2012 (such as 2010, 2011 etc).
    I don't want to expand all the years, but only the one I clicked. Anyone knows what is the solution for that.
    I am sorry that I cannot attach any screen shots to explain better.
    I would really appreciate your help on this.
    Thanks
    JD

    I am using OBIEE 11.1.1.7.0.
    ok, I tried one hierarchy per dim table but didn't work too. When I created a logical column as Month + Year (e.g Jan 2014) and added to the hierarchy, its working fine. But that creates other issues on the
    client side.
    I guess, since the month is common to all the years its expanding when clicked. I also added month and year as "chronological key", but that didn't work either.
    Please advice any solution if you know it.

  • URGENT Please help ! Amount from BET01

    Hello,
    Can anyone please tell me how to get the right amount from BET01 field in infotype 0008?
    when i m reading the table pa0008 using select statement, my value in the internal table shows that bet01 field for hourly employees is multiplied by a factor of 10. But i am seeing the original amount in pa0008 (which i see in infotype, without multipilication of 10).
    I tried using RP_FILL_WAGE_TYPE_TABLE. But i have same problem. Again, this is only for some employees, not for all. Could you please suggest what factors I should investigate to solve this problem?
    Thank you in advance for your help!
    Rushi

    The Currency Field determines how it is going to be displayed.  This confused me for the longest.  In our system hourly rate looks like 1471.78 which makes you think that they are getting paid $1471.78 per hour, but the Currency is USDN which means that the system shifts the decimal point two places to the left so the salary is actually $14.7178 per hour.  I hope this helps.

  • F4 help for field along with description

    Hi,
    I have given fixed values and thier description for a domian. I am getting F4 help because of them. But in F4 help i am getting description of the fixed value instead of fixed value.
    One more thing is, I want to display both value and description in F4 help, when I select any entry from  the pop up table i sholud get only value but I am getting description here again. This i have done using a value table.
    How can I get F4 help with both value and description and after selection only value should get selected.
    Thank you,
    Regards,
    Kusuma K.

    HI,
    Try using this FM - F4IF_INT_TABLE_VALUE_REQUEST.
    You can use the below code for reference.
    REPORT  Z_F4_VAL_DESCRIPTION.
    DATA: is_Planning_plant TYPE t399i OCCURS 0.
    DATA: is_return TYPE ddshretval OCCURS 0,
         w_return LIKE LINE OF is_return.
    PARAMETER : p_mpla TYPE iloa-swerk.
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_mpla.
    IF is_Planning_plant[] IS INITIAL.
    SELECT * FROM t399i INTO TABLE is_Planning_plant.
    ENDIF.
    CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
    retfield = 'IWERK'
    dynpprog = sy-repid
    dynpnr = sy-dynnr
    dynprofield = 'P_MPLA'
    value_org = 'S'
    TABLES
    value_tab = is_Planning_plant
    return_tab = is_return.
    IF sy-subrc EQ 0.
    READ TABLE is_return INTO w_return INDEX 1.
    IF sy-subrc EQ 0.
    p_mpla = w_return-fieldval.
    ENDIF.
    ENDIF.
    Regards,
    Snehal

  • Help with amount of songs

    i have a 5th generation iPod that has a memory of 30GB/7500 songs PC + Mac i only have 491 songs on it and it says the memory is all used up and there is no more room for more songs......my friends have the same ipod and have about 6/7 of space left and have more songs than me....i only have songs on my iPod and every song is less than 9 minutes.......does resetting my iPod help it or no? can any1 help me please?!?!
    Dell   Windows XP  

    do you have several DVD's on your iPod? Those eat up a ton of space...
    Also, look at your music files and see how many KB a piece they are, if any are over 10KB, then you have some huge song files, and as the other guy said, make sure that youproperly imported the files

  • HELP on Amount of use with RSL's ?

    i want to develop product and want to use the RSL (Runtime
    Shared Library) of the Flex. i don't know the amount of use with
    this feature and i will be happy to get info on the amount of site
    that use this.
    it critical for the future of this project.
    we want that a lot of users that will come to our site will
    already have the RSL on their Flash Player.

    If you want to know how many people have framework caching
    enabled (or how many people have the flex framework RSL, refer to
    this
    flexcoders
    message thread.
    Instead if you wanted to know how many people use RSLs (not
    flex framework) in general, flexcoders would be the best place for
    such a poll.

Maybe you are looking for

  • Is it possible to create a form that allows users to import data from an excel spreadsheet

    I have not been able to start my reading on scripting or other livecycle features to be able to try to figure this out but I was wondering if anyone else had ever created a form with this feature or had any ideas of how it would be possible.  Our com

  • Usb Disabled

    Hi, Last night I put my computer to sleep and I woke up this morning to the error message saying "Because a USB device was drawing too much power from your computer, one or more of your USB devices have been disabled. To prevent damaging your compute

  • Insert Query in JDBC

    How to write sql insert statement if the code is like this: String Str1 = "abhishek" String St21 = "Agrawal" stmt.executeUpdate("insert into my_table values( \"str1\",\"str2\")"); this is not working help ME!!!!!

  • Can execute query be controlled for blocks

    Hi, I've 3 blocks in my form. The first and the second block are in query mode, therefore I cannot update, insert, delete values here. The third block, I have put on another canvas. So the moment there's data in the second block the third block gets

  • Steps for creating pop-up text on a specific customer when creating a order

    We want to be able to have a pop-up text for some customers, for instance informing the user to mark the order a special way. How is this done? Also, if we want for some customer always mark the PO number (VBKD-BSTKD) with the same reference, how can