sum of hierarchy

Hi all
create table tdata 
( 
PERSON_ID number, 
ORG_ID number, 
TIMEE number
);

insert into tdata (PERSON_ID, ORG_ID, TIMEE)
values (363, 1529, 0);

insert into tdata (PERSON_ID, ORG_ID, TIMEE)
values (363, 1892, 0);

insert into tdata (PERSON_ID, ORG_ID, TIMEE)
values (597, 309, 20);

insert into tdata (PERSON_ID, ORG_ID, TIMEE)
values (597, 905, 20);

insert into tdata (PERSON_ID, ORG_ID, TIMEE)
values (4730, 905, 20);

create table thierarch
(
ORG_ID number, 
LV number, 
PARENT_ORG_ID number
);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (167, 3, 0);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (172, 3, 0);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (905, 1, 1489);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (176, 2, 172);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (309, 1, 176);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (1489, 2, 167);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (1892, 1, 168);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (168, 2, 167);

insert into thierarch (ORG_ID, LV, PARENT_ORG_ID)
values (1529, 1, 168);
with t as
 (select org_id, lv, parent_org_id, 'ORG' who_type, 0 timee
    from thierarch
  connect by parent_org_id = prior org_id
   start with parent_org_id = 0
  --
  union all
  --
  select p.person_id, 0, o.org_id, 'PER', p.TIMEE
    from (select *
            from thierarch
           where lv = 1
          connect by parent_org_id = prior org_id
           start with parent_org_id = 0) o,
         (select * from tdata) p
   where o.org_id = p.org_id)
--
select rownum,
       org_id,
       who_type,
       parent_org_id,
       t.lv,
       TIMEE,
       (select sum(tt.TIMEE)
          from t tt
         start with tt.org_id = t.org_id
        connect by prior tt.org_id = tt.parent_org_id) as TIMEE
  from t
connect by parent_org_id = prior org_id
 start with parent_org_id = 0;
gives
 
    ROWNUM     ORG_ID WHO_TYPE PARENT_ORG_ID         LV      TIMEE      TIMEE
---------- ---------- -------- ------------- ---------- ---------- ----------
         1        167 ORG                  0          3          0         40
         2       1489 ORG                167          2          0         40
         3        905 ORG               1489          1          0         40
         4       4730 PER                905          0         20         20
         5        597 PER                905          0         20         40
         6        168 ORG                167          2          0          0
         7       1892 ORG                168          1          0          0
         8        363 PER               1892          0          0          0
         9       1529 ORG                168          1          0          0
        10        363 PER               1529          0          0          0
        11        172 ORG                  0          3          0         20
        12        176 ORG                172          2          0         20
        13        309 ORG                176          1          0         20
        14        597 PER                309          0         20         40
 
14 rows selected
 
SQL> 
but I want to
 
    ROWNUM     ORG_ID WHO_TYPE PARENT_ORG_ID         LV      TIMEE      TIMEE
---------- ---------- -------- ------------- ---------- ---------- ----------
         1        167 ORG                  0          3          0         40
         2       1489 ORG                167          2          0         40
         3        905 ORG               1489          1          0         40
         4       4730 PER                905          0         20         20
         5        597 PER                905          0         20         20
         6        168 ORG                167          2          0          0
         7       1892 ORG                168          1          0          0
         8        363 PER               1892          0          0          0
         9       1529 ORG                168          1          0          0
        10        363 PER               1529          0          0          0
        11        172 ORG                  0          3          0         20
        12        176 ORG                172          2          0         20
        13        309 ORG                176          1          0         20
        14        597 PER                309          0         20         20
 
14 rows selected
 
SQL> 
so for anyone 597 must be 20 because salvation works in two different org

What did I miss?

Hello

The scalar subquery that calculates an incorrect value is looking for a specific org_id in result set t, but org_id is not unique. It looks like the combination of org_id and parent_org_id is unique, so if you want to identify the lines, you must compare the two values.

with t as
 (select org_id, lv, parent_org_id, 'ORG' who_type, 0 timee
    from thierarch
  connect by parent_org_id = prior org_id
   start with parent_org_id = 0
  --
  union all
  --
  select p.person_id, 0, o.org_id, 'PER', p.TIMEE
    from (select *
            from thierarch
           where lv = 1
          connect by parent_org_id = prior org_id
           start with parent_org_id = 0) o,
         (select * from tdata) p
   where o.org_id = p.org_id)
--
select rownum,
       org_id,
       who_type,
       parent_org_id,
       t.lv,
       TIMEE,
       (select sum(tt.TIMEE)
          from t tt
         start with tt.org_id = t.org_id
            AND  tt.parent_org_id     = t.parent_org_id     -- *****  NEW  *****
        connect by prior tt.org_id = tt.parent_org_id) as TIMEE
  from t
connect by parent_org_id = prior org_id
 start with parent_org_id = 0;

I added only 1, line 5 from the end.
Depending on your data and your needs, you may need to do the same kind of change elsewhere in the query, where org_id is used.

This whole request seems unnecessarily complicated. I don't know what you're trying to do, but it is difficult to imagine anyting you are trying maybe that we can't do with 1 or 2 possible, CONNECT BY query, and not 4 as you use.

Tags: Database

Similar Questions

  • Get the sum of all Member of a hierarchy.

    Hello
    I want to get the sum of each Member in a hierarchy.
    The hierarchy is defined in the strdet table:
    create table strdet
    (costcenterms varchar2(20),     // parent
    costcenterdet varchar2(20),    // child
    lev varchar2(1))
    The values for each object/material by costcenter (child) is defined in the details_det table:
    create table details_det
    (costcenterms varchar2(20),
    eppid varchar2(30) ,
    purchcontyear0 number(4,1) )
    Some examples of data:
    insert into strdet values ('1' , '1.1','2')
    /
    insert into strdet values ('1' , '1.2','2')
    /
    insert into strdet values ('1.1' , '1.1.1','3')
    /
    insert into strdet values ('1.1' , '1.1.2','3')
    /
    insert into strdet values ('1.2' , '1.2.1','3')
    /
    insert into strdet values ('1.2' , '1.2.2','3')
    /
    insert into strdet values ('1.2' , '1.2.3','3')
    /
    insert into strdet values ('1.1.1' , '1.1.1.1','4')
    /
    insert into strdet values ('1.1.1' , '1.1.1.2','4')
    /
    insert into strdet values ('1.1.2' , '1.1.2.1','4')
    /
    insert into strdet values ('1.2.1' , '1.2.1.1','4')
    /
    insert into strdet values ('1.2.1' , '1.2.1.2','4')
    /
    COMMIT;
    insert into details_det values('1.1.1.1','epp1',10);
    insert into details_det values('1.1.1.1','epp2',20);
    insert into details_det values('1.1.1.1','epp3',0);
    insert into details_det values('1.1.1.2','epp1',0);
    insert into details_det values('1.1.2.1','epp2',5);
    insert into details_det values('1.1.2.1','epp4',15);
    insert into details_det values('1.2.1.1','epp1',65);
    insert into details_det values('1.2.1.1','epp2',95);
    insert into details_det values('1.2.1.2','epp1',5);
    commit;
    The desired sql stmt output should be like this:
    costcenter             val
    --------------             ------
    1                        220
    1.1                       55
    1.2                     165
    1.1.1                    30
    1.1.2                    20
    1.2.1                  165
    I wrote the following, so far...
    SQL> select distinct s.costcenterms , sum(purchcontyear0) over(partition by s.costcenterms order by s.costcenterms)
      2        from details_det d , strdet s
      3        where s.costcenterdet=d.costcenterms(+)
      4        start with s.costcenterms='1'
      5             connect by  s.costcenterms = prior s.costcenterdet
      6        order by s.costcenterms
      7  /

    COSTCENTERMS                                                 SUM(PURCHCONTYEAR0)OVER(PARTIT
    ------------------------------------------------------------ ------------------------------
    1.2                                                         
    1.2.1                                                                                   165
    1.1.1                                                                                    30
    1.1.2                                                                                    20
    1                                                           
    1.1                                                         

    6 rows selected
    How should I modify the above sql stmt to get the result you want...?

    Note: I use OracleDB 10 g. v.2

    Thank you very much
    SIM

    sgalaxy wrote:
    Anyway, since I want to use the sql stmt to define a materialized view, all versions of data of hierarchical queries are not allowed... (oracle ora-30361 error...).

    No error on my:

    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    
    SQL> create materialized view x_mv
      2  as
      3  select  grp,
      4          sum(purchcontyear0)
      5    from  (
      6           select  connect_by_root s.costcenterms grp,
      7                   d.purchcontyear0
      8             from  strdet s,
      9                   details_det d
     10                   where s.costcenterdet=d.costcenterms(+)
     11                   connect by s.costcenterms = prior s.costcenterdet
     12          )
     13    group by grp
     14  /
    
    Materialized view created.
    
    SQL> select  *
      2    from  x_mv
      3    order by grp
      4  /
    
    GRP                  SUM(PURCHCONTYEAR0)
    -------------------- -------------------
    1                                    215
    1.1                                   50
    1.1.1                                 30
    1.1.2                                 20
    1.2                                  165
    1.2.1                                165
    
    6 rows selected.
    
    SQL> 
    

    SY.

  • A slightly different running sum in a query of the hierarchy

    Hello

    I've stuck with this problem for some time, so pls help:

    I'm basically trying rollup/aggregate values from the lower level nodes for the upper level nodes in a hierarchy. But this aggregate is a little different from a simple cumulative values.

    create the table ent_rel (parent number, number of children, number of child_amount);

    insert into ent_rel
    Select null, 100, the double null
    Union all select 100, 101, the double null
    the Union all select 101, 102, 20 of the double
    Union all select 102, 103, the double null
    Union all select 103, 104, the double null
    Union all select 104, 105, the double null
    the Union all select 105, 106, 40 of the double
    Union all select 106, 107, the double null
    the Union all select 107, 108, 20 of the double
    the Union all select 107, 109, 10 of the double
    Union all select 101, 203, the double null
    the Union all select 203, 205, 50 of the double
    Union all select 205, 207, the double null
    Union all select 207, 209, the double null
    the Union all select 209, 210, 10 of the double
    ;

    commit;

    Select RPAD (' ', (LEVEL - 1) * 2, '-'). child child
    level
    child_amount
    of ent_rel
    Start with the parent is set to null
    connect by prior child = parent

    CHILD_AMOUNT LEVEL CHILD
    --------------------------------- ------ -------------
    1 100
    --101 2
    ---102 3 20
    ---103 4
    ---104 5
    ---105 6
    ---106 7 40
    ---107 8
    ---108 9-20
    ---109 9 10
    ---203 3
    ---205 4 50
    ---207 5
    ---209 6
    ---210 7 10

    Now, I need a SQL query or even a simple PL/SQL code to get the field DESIRED_AMOUNT as shown below:

    Please note that there could be many more branches parallel to a node, but I kept simple just to get the concept.

    DESIRED_AMOUNT CHILD_AMOUNT LEVEL CHILD
    --------------------------------- ------ ------------- --------------
    100 1 70
    --101 2 70
    ---102 3 20 20
    ---103 4-40
    ---104 5 40
    ---105 6 40
    ---106 7 40 40
    ---107 8 30
    ---108 9 20 20
    ---109 9 10 10
    ---203 3 50
    ---205 4 50 50
    ---207 5 10
    ---209 6 10
    ---210 7 10 10

    Thank you very much in advance.

    Thank you and best regards,
    In.
  • Formula member to sum based on a model

    Hello

    I have a question by creating a formula for a sum based on a model member.

    In a cube, OSI, we have 2 sparse dimensions, which contains a hierarchy of alternative with shared members. There is a hierarchy of high maintenance new shared members are added each month.

    I would like to remove this successor to the shared members hierarchy and replace it with a formula of Member.  This hierarchy looks like this

    Entity CC01 Summation

    CC-01-100

    CC-01-101

    CC-01-111

    etc.

    I tried to add a member formula that corresponds to this model @MATCH ('Entity', ' CC-01 * ") and summarizes all CC - 01 *.  My formula looks like this @SUM (@MATCH ('Entity', ' CC-01 * ")).  It does not work.  She added a few other totals which I have not yet studied.

    Any idea is appreciated...

    Thank you.

    Hello

    It is not necessary to take the name of the dimension of the mbrname. @MATCH (mbrName | genName | levName, 'boss')

    Take the parent where the search should begin here. So, without other hierarchies.

    It should work.

    Kind regards

    Philip Hulsebosch

  • Can I refer to a rangelist in a hierarchy of shared member?

    I would like to make the sum of a series of accounts based on one hierarchy, not the main hierarchy.  In the hierarchy of the main, they are 1,5,4,3,2, and in the alt hierarchy, they are shared in the good order of 1,2,3,4,5.  If I reference the rank 1:3, how can you ensure that I get 1,2,3,4 and not 1,5,4,3?

    Background:

    Basically, I'm trying to map a sum of numerical range of account numbers in general ledger to another account of management based on the members of the other 2 dimensions.  The main problem I have is that, as I have not found the function that can sort in alphanumeric order a list within a selection of members, and my GL accounts are not in the order in the hierarchy of main due to a reorganization of their parents, I loaded as a sorted list of platform of shared to another hierarchy members.  Now, I just @LIST (1:3, [the optional to use as reference parent node]) function.  I guess another way of asking the question might be: how to reference the member names in Essbase as digital beaches as you would in an ERP?

    Maybe there is another way and I approach it all wrong?

    Essbase v11.1.2.3.507 (under planning)

    So what you really want is to extract all accounts with a number between e.g. 1 and 4 (even if the same parent has other children of 5, 6, 7 and so on)?

    @BETWEEN would do in this case.

  • Beginner problems - how to make the sum of the lists in a drop-down table

    Hello

    Please forgive my newness but I'm creating a table with 10 + lines and columns and need to calculate the sum of the columns. The user must select a number from a drop-down list and I would like to summarize these nbumbers at the bottom of the column, seems simple enough...

    I have named each cell in the links tab but can't seem to find the script of the sum on the right. I just started using livecycle and have had a good experience so far.

    I wish it was like excel where you just need to click or highlight the cells you want to summarize and do with it but I can't seem to find the easy button...

    You can not simply highlight a column, name it and then use that name in a script to sum?

    Thanks a lot for all the help and I have looked around, but can't get a grip on this one, so I apologize if this is requested before.

    Go easy on me.

    You have to be very accurate with referencing the fields or the formulas will not work. If you have renamed the fields of their default values, then you should reference them as you named the.

    Here's an example of my hierarchy, called as you explained:

    In this case, the use of scripts on the Total field will look like this:

    this.rawValue = (Row1.A1.rawValue * 1) + (Row2.B1.rawValue * 1) + (Row3.C1.rawValue * 1) + (Row4.D1.rawValue * 1);

    So that when I switch to preview, it behaves correctly:

  • query rewite enable cube + jump level hierarchy problem

    Hello

    can someone help me on the following scenario, please?

    (I use OWB 11.2 client on WIN XP)

    I just created a dimension, i.e. dim_1 with three levels (l1, l2 and l3) with the ' ROLAP: with Cube MV ' storage option. the business identifier is of type number named 'key '. The standard hierarchy of the dimension is
    by default except that I have defined L1 level as him "jump at the level" of the L3 level. As I'd hoped associates the modified dimension table so that the key added to level L3 as his parents another level L1.

    Then I designed a map of ETL to load the following values:
    Hierarchy (Default): L1-L2-L3 > >
    Keychain attr. : 1-> 2-> 3
    and
    hierarchy (using hierarchical): L1-L3 >
    Keychain attr. : 1 > 4



    This means that the '1' L1 button is parent of key '3' or '4' of L3.
    Then I created a single dimension cube with the "ROLAP: with the Cube of MV" storage option. The only measure of the cube is named "N" of type number. The level of the dimension of the cube is L3 as a default value. Then I designed a mapping of ETL to load following values in this measure:
    « N » - « L3 ». "" Key.
    __________________
    1 - 3
    2 - 4

    When I update the related materialized view of the cube it does return no rows.

    * 1. why what is happening? *

    But when firstly, I refresh the MV associated with the dimension, and then refresh the cube MV, it returns the following incorrect result:
    Level.Key - N - County
    ___________________________
    L2.2 - 2-1
    L1.1 - 2-1

    The correct result should be:

    Level.Key - N - County
    ___________________________
    L2.2 - 1-1
    L1.1 - 3-2

    * 2. why what is happening? *

    When I got Disabling query rewrite of the cube, then there is no need to be refreshed dimension MV first. Refreshing Cube MV returns the following results about:

    Level.Key - N - County
    ___________________________
    L2.2 - 1-1
    L1.1 - 3-2
    L2. -2-1

    * 3.Is it bound to the ENABLE QUERY REWRITE of cube MV property? *

    Thanks in advance,

    SMSK.

    Command is not supported in cube MVs, see the documentation for OLAP;
    http://docs.Oracle.com/CD/E11882_01/OLAP.112/e17123/cubes.htm#CHDHBGGB

    A cube must conform to these requirements, before it can be referred to as a cube materialized view:
    * All the dimensions of the cube have at least a level and a level-based hierarchy. Ragged and non-hierarchical hierarchies are not supported. The dimensions should be mapped.
    * All dimensions in the cube using the same aggregation operator, which is SUM, MIN, or MAX.
    * The cube has one or more dimensions and one or more measures.
    * The cube is fully defined and mapped. For example, if the cube has five steps, then all five are mapped to the source tables.
    * The data for the cube type is NUMBER, VARCHAR2, NVARCHAR2, or DATE.
    * Source detail tables support dimension and count constraints. If they have not been defined, then use the Advisor to relational schema to generate a script that defines on detail charts.
    * The cube is compressed.
    * The cube can be accompanied by calculated measures, but he can not support analytics more advanced in the cube script.

    See you soon
    David

  • Hierarchical queries with Rollup sum (CONNECTED BY GROUP BY ROLLUP)

    Hi all

    Imagine the following scenario: I have an ACCOUNT table that contains the accounts and their hierarchy (currently 5 levels) and a BALANCE table that holds the record for the balance of the accounts. Only CHILD accounts (level 5) have records in the table for BALANCE. Simple example:
    CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
    CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
    INSERT INTO ACCOUNTS VALUES ('TOT',NULL,'Total');
    INSERT INTO ACCOUNTS VALUES ('ANA1','TOT','General Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801001','ANA1','Small Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801002','ANA1','Transportation');
    INSERT INTO ACCOUNTS VALUES ('ANA2','TOT','Health Expenses');
    INSERT INTO ACCOUNTS VALUES ('4802001','ANA2','Healthcare');
    INSERT INTO ACCOUNTS VALUES ('4802002','ANA2','Facilities');
    
    
    INSERT INTO BALANCES VALUES ('4801001', 2000);
    INSERT INTO BALANCES VALUES ('4801002', 1000);
    INSERT INTO BALANCES VALUES ('4802001', 3000);
    INSERT INTO BALANCES VALUES ('4802002', 4000);
    What I need in this scenario is to run a hierarchical query, where each node, I calculate the sum of all its children (in TERMINAL nodes that are child accounts, this amount is the value of the balances itself). End result would be:
    TOT -> 10000
      ANA1 -> 3000
        4801001 -> 2000
        4801001 -> 1000
      ANA2 -> 7000
        4802001 -> 3000
        4802002 -> 4000
    I tried many ways and found a solution that works for a fixed amount of levels, basically he built the hierarchy and calculates the SYS_CONNECT_BY_PATH, then divides it as a regular expression and using GROUP BY ROLLUP to calculate the highest levels. Then I assemble again, now with the calculated values. Here's the example query:
    select level
        , NVL (vfinal.child_account,'TOTAL') ||' - '||
                            ( SELECT account_desc
                                FROM accounts 
                               WHERE account_code = vfinal.child_acct ) account_name
    
         , to_char(sum_bal, 'fm999g999g999g990') as rolled_up_balance
      from 
    (
    select coalesce( princ.lvl3, princ.lvl2, princ.lvl1 ) child_acct
         , DECODE ( princ.lvl2 , NULL 
                                     , NULL 
                                     , DECODE ( princ.conta_lvl3, NULL
                                     , princ.conta_lvl1,princ.conta_lvl2 ) ) parent_acct
         , sum(princ.balance_amount) sum_bal
    from (
    select hier.lvl1
         , hier.lvl2
         , hier.lvl3
         , hier.parent_account
         , hier.account_code child_acc
         , bal.balance_amount
      from ( select level  
                  , sys_connect_by_path( account_code, '/' ) hierarchy_acct
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,3) lvl3
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,2) lvl2
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,1) lvl1
                  , account_code
                  , parent_account  
               from accounts acc
               where level <= 3
               start with parent_account is null
               connect by nocycle prior account = parent_account
               order siblings by parent_account
               ) hier
          , balances  bal
      where bal.cod_conta  = hier.account_code
    ) princ
    where princ.lvl1 is not null
    group by rollup ( princ.lvl1
                    , princ.lvl2
                    , princ.lvl3 )
    
    order by princ.conta_lvl1
           , princ.conta_lvl2
           , princ.conta_lvl3
    ) vfinal
    where child_acct is not null
    start with parent_acct is null
    connect by nocycle prior child_acct = parent_acct
    All is said and done, what I need is to do the same thing for infinite levels, because this query has 3 fixed levels. Do you know how can I structure a new query where, regardless of the number of levels, amounts of parent are all wound like that?

    Thank you very much in advance! Best regards!
    Thiago

    Published by: Thiago Sep 6, 2011 11:31

    Published by: Thiago Sep 6, 2011 13:01
    select  account_code,
            (
             select  sum(balance_amount)
               from  accounts a2,
                     balances b
               where b.account_code(+) = a2.account_code
               start with a2.account_code = a1.account_code
               connect by a2.parent_account = prior a2.account_code
            ) balance_amount
      from  accounts a1
    /
    
    ACCOUNT_CODE    BALANCE_AMOUNT
    --------------- --------------
    TOT                      10000
    ANA1                      3000
    4801001                   3000
    4801002
    ANA2                      7000
    4802001                   7000
    4802002
    
    7 rows selected.
    
    SQL> 
    

    SY.

  • A rendering based on a hierarchy level group

    Hello
    I am faced with a request that I would like a report based on the selection of a group hierarchy level.

    Here's the situation: I have a table with a 'normal' hierarchy, said the Scott, with his EMPNO and MGR columns EMP table. What I would like to have is a report that gives me the sum of the column group SAL by all members at a given level. So, if I choose the LEVEL = 1, Id like to see the total number if I choose LEVEL = 2, I would like to see the sum of the salary of all employees managed by the respective Manager etc..

    I know that there are opportunities to denormalize the table first and try to solve it from there, but I want to know is if everyone is aware of a more generic way to achieve this? I could accept is a limitation hardcoded the number of nested levels, as seems to be very difficult to achieve in SQL.

    In the real world scenario, I use only balanced trees, so from this point of view, the reports will be compatible.

    Any ideas?

    Thanks, Jürgen

    http://www.orafaq.com/node/55

    analytical functions will answer your questions...

  • To calculate the sum Vals and LV is higher than its target line LV

    When I answered this thread of application of the hierarchy and the number of subnodes
    I made this question.

    I use Oracle11gR2.

    I want the sum Vals and LV is superior to his LV of the target line to the command by sorkKey.
    Ex if LV 2, sumVal is the sum of this line and another line which LV > 2.
    MyTable
    sortKey  LV  Val
    -------  --  ---
          1   2   10
          3   3   20
          5   4   30
          6   3   40
          7   4   50
          9   2   60
         15   3   70
         16   4   80
    with MyTable(sortKey,LV,Val) as(
    select  1,2,10 from dual union
    select  3,3,20 from dual union
    select  5,4,30 from dual union
    select  6,3,40 from dual union
    select  7,4,50 from dual union
    select  9,2,60 from dual union
    select 15,3,70 from dual union
    select 16,4,80 from dual)
    expected Output
    sortKey  LV  Val  sumVal
    -------  --  ---  ------
          1   2   10     150   (10+20+30+40+50)
          3   3   20      50   (20+30)
          5   4   30      30   (30)
          6   3   40      90   (40+50)
          7   4   50      50   (50)
          9   2   60     210   (60+70+80)
         15   3   70     150   (70+80)
         16   4   80      80   (80)
    If you use subQuerys, the solution is below.
    But it's very complex :-(
    My question is "is it more simple solution?
    For ex using fuction OLAP, model clause etc...
    with MyTable(sortKey,LV,Val) as(
    select  1,2,10 from dual union
    select  3,3,20 from dual union
    select  5,4,30 from dual union
    select  6,3,40 from dual union
    select  7,4,50 from dual union
    select  9,2,60 from dual union
    select 15,3,70 from dual union
    select 16,4,80 from dual)
    select sortKey,LV,Val,
    (select sum(b.Val)
       from MyTable b
      where a.sortKey <= b.sortKey
        and b.sortKey <
            (select nvl(min(c.sortKey),99999)
               from MyTable c
              where c.sortKey > a.sortKey
                and c.LV <= a.LV)) as sumVal
      from MyTable a;

    In fact, it can be simpler:

    with t as (
               select  1 sortkey,2 lv,10 val from dual union all
               select  3,3,20 from dual union all
               select  5,4,30 from dual union all
               select  6,3,40 from dual union all
               select  7,4,50 from dual union all
               select  9,2,60 from dual union all
               select 15,3,70 from dual union all
               select 16,4,80 from dual
              )
    select  sortkey,
            lv,
            val,
            sum(val) over(order by sortkey range between current row and rng following) sumval
      from  (
             select  sortkey,
                     lv,
                     val,
                     nvl(
                         (select min(b.sortkey) from t b where b.sortkey > a.sortkey and b.lv <= a.lv) - a.sortkey - 1,
                         max(sortkey) over() - a.sortkey
                        ) rng
               from  t a
             )
      order by sortkey
    /
    
       SORTKEY         LV        VAL     SUMVAL
    ---------- ---------- ---------- ----------
             1          2         10        150
             3          3         20         50
             5          4         30         30
             6          3         40         90
             7          4         50         50
             9          2         60        210
            15          3         70        150
            16          4         80         80
    
    8 rows selected.
    
    SQL> 
    

    SY.

  • calculation of Salary to the top or to the bottom of the hierarchy

    Hello all,.

    How to get the total salary wound of lower level to a higher level of the hierarchy?

    We can get the hierarchy of the employee and the salary of hr.employees table using the suite of applications:
    select level, last_name, salary
    from hr.employees
    start with employee_id = (select employee_id
                                         from  hr.employees
                                         where manager_id IS NULL
                                        )
    connect by prior employee_id  = manager_id
    How to get the salary rolled up to a higher level with sql as follows?
    level      last_name        salary        total salary

    2         Kochhar           17000          109800
    3         Whalen              4400            4400
    3         Mavris                6500            6500
    3         Bae                 10000           10000
    3         Higgins             12000          20300
    4             Gietz              8300           8300
    3         Greenberg        12000          51600
    4            Faviet             9000            9000
    4            Chen              8200            8200
    4            Sciarra            7700             7700
    4            Urman            7800             7800
    4            Popp              6900             6900
    Here all level 4 level 3 Greenberg rolled up to level 3, then level 4 under Higgins rolled up to level 3, then all the level 3 under Kochar wrapped at level 2.

    Is it possible in sql?

    Thanks for your help.

    Edited by: rxshah May 14, 2010 03:45

    (B-)

    select level, last_name, salary,
    (select sum(b.salary) from hr.employees b
     start with b.employee_id=a.employee_id
     connect by prior b.employee_id  = b.manager_id)
    from hr.employees a
    start with employee_id = (select employee_id
                                         from  hr.employees
                                         where manager_id IS NULL
                                        )
    connect by prior employee_id  = manager_id;
    
  • OBIEE BI answers: measures bad aggregation at the top level of the hierarchy

    Hi all,
    I have the following problem. I hope to be clear in my English because it is somewhat complicated to explain.

    I did following:

    ID of drugs classified in quantity
    1 9
    2 4
    1 3
    2 2

    and drugs following table:

    Drug brand Id brand Description drug whose active ingredient Id drug whose active principle Description
    Aulin Nimesulide 1 1
    2 Asprina 2 Acetilsalicilico

    In AWM, I've defined a Dimension of drugs based on the following hierarchy: drug whose active ingredient (parent) - brand name of medication (sheet) mapped as Description:

    The active ingredient of drug = drug Active ingredient Id of my Table of drugs (pharmaceutical = Description of the active ingredient LONG DESCRIPTION attribute)
    Pharmaceutical brand Description = drug brand Id of my drugs Table (LONG DESCRIPTION = Description of drug brand attribute)

    Indeed, in my cube, I have traced pharmaceutical leaf-level brand Description = Id of the drug of my fact table. In AWM drugs Dimension is mapped as Sum aggregation operator

    If I select on answers drug whose active ingredient (parent of my hierarchy) and the quantity, in my view, after the result

    Description of the active ingredient drugs classified in quantity
    Acetilsalicilico 24
    Nimesulide 12

    indeed of the correct values

    Description of the active ingredient drugs classified in quantity
    Acetilsalicilico 12
    Nimesulide 6

    EXACTLY the double! But if I dig drug Description of the active ingredient Acetilsalicilico I can't see correctly:

    Drug whose active ingredient Description pharmaceutical brand classified in quantity Description
    Acetilsalicilico
    -12 Aspirina
    Total 12

    Aggregation of evil is only at the top level of the hierarchy. The aggregation on the lower level of the hierarchy is correct. Perhaps the answers also amount Total line? Why?

    I'm frustrated. I ask your help, please!

    Giancarlo

    OK your on 10G but the view of the cube and the obligation to limit the levels in the LTS in the RPD is valid in both.
    I think we found the problem,
    Go to each source logical table this logic table (x 2 in your case you have two levels) and on the content tab, window background ' use this "Clause Where" filter to limit the rows returned. »
    Open the expression builder, locate LEVEL_NAME according to your cube and limit accordingly, it is to say LEVEL_NAME = "BRAND_DESCRTIPION" for the aggregation BRAND_DESCRIPTION LTS and LEVEL_NAME = "XXXX" in detail, SFF, where XXXX is the name of level of hierarchy in your cube for details (leaves) records.

    Can you try that and let us know?
    Thank you
    Alastair

  • Formula in the primary account not to ASO dimension hierarchy

    Is this in any way by which I can write a formula in a primary account not dimension hierarchy. I have tagged this member as allowing multiple hierarchies and I can't adjust primary hierarchy as dynamic to ASO. so I put it as stored and now I want to write a formula for one of the members. I'm not able to put the formula for stored dimension.

    is there a workaround or can you please suggest me something that might work

    Thank you

    You can have formulas in the dimensions (or hierarchies) marked as dynamic. If you have a dynamic hierarchy, you might substitute in there running sums.

  • SUM IS INCORRECT

    I have a worksheet and the SUM function gives the wrong number. It's really dangerous because I need precision.

    I deduced that there is a single cell, that it fails to include in the sum, even if the cell is selected.

    I click on the cell, I want to add to the column, then press = and then select the column. The sum is incorrect and fails to add one of the cells

    Why?

    It is so irritating and so dangerous that I need clarification on this sheet of numbers. Never had this with excel.

    Help, please!

    Thank you

    without details, it is difficult to guess.

    Often, the cell is not the same as the other somehow.  Maybe it's in text format, or there's a funny character in the cell.  Try to copy the data to a new table in a new document to check the results.

    If you can validate the data, then we can try too.

  • sum of only of positive numbers

    I'm the Dean in a high school and I need to keep track of demerits of the student and served detention.  I created a spreadsheet on the photo below.  As you can see whenever a student gets a demerit, I put it all in a row (date, teacher gives the incapacity, that was the incapacity and how many demerits received for the offence).  So you can imagine the end of the quarter, this worksheet is big enough (400-500 lines). I also put a negative number (-2) when the student uses of detention (a student must serve each received 2 drawbacks.

    I wrote about this formula on this forum almost two years ago.  I have another tab (sheet) that adds up all the disadvantages, so I can go to this (roadmap) tab and see who needs to serve.  The formula a = SUMIF (Sheet1! Header, header, Sheet1H:H) and it works beautifully. This other sheet has our entire student body on it (each student only listed time... If we have a student population of 300 students, it has only 300 lines). We add all the points (even negative), I can see that has more than 2 demerits and who needs to serve.  In the example below, Steve Abrahim would have 0 demerits on the second sheet because he received 4, but also served 4.

    But now, I realize that I need a second tab (sheet) that has the total number of demerits a student receives during the quarter.  Then, I need a formula that sums only positive numbers.  So, once again, this worksheet will have all our student body listed in the column (a student by rank).  On this tab (sheet), Steve Abrahim would be 4 demerits (even if he served two detentions) and Adam Butler would also have four (even if he served only a detention).  Can someone write this formula?

    Please let me know if you need any clarification on my question.

    Thank you

    Mark

    = SUMIF (E, ' > = 0 "" ")

    where I think the Points column is E

Maybe you are looking for

  • I can't get my new MacBook Air operational

    Hi, I bought a new Apple MacBook Air yesterday of FNAC in Geneva, and when I got it home I couldn't go beyond the black bar. In the end, I got a white circle with a slash and a black screen. Please tell us what to do. Thank you.

  • Time Machine works do not successfully (I think) restore on a new hard drive (El Capitan)

    I had the hard drive replaced in my mid-2011 iMac 27 "a couple of days. Everything seems to go well with the Time Machine restore process. But it does not appear that all successful backups have occurred within 36 hours since. If a block of half day

  • After BIOS update of my Satellite A100 himself lock

    I have a big problem with my Toshiba Satellite A100 laptop.I work with windows Vista Updates my system ha been doneAnd I think that it upgraded the BIOS.Now I can't open my Tower because I don't know the pass. Please help me solve this problem

  • DAQmx autorange

    Hi all I use a 6363 OR USB and currently facing a problem on all my data. I'm sampling of data between 10 v and - 10 V (basically a laser moves and tensions are at a different distance from the laser measuring on a range of 5 mm, distance converted i

  • Windows Vista system restore problems

    Hello I picked up a nasty virus on my laptop and have all sorts of questions to get rid of him. I tried to perform a restore of the system in safe mode, but there is a major problem: there is no 'System Protection' tab on the box 'system properties '