Thread: Aggregation with a parent child hierarchy


Permlink Replies: 10 - Pages: 1 - Last Post: Dec 7, 2007 2:47 PM Last Post By: Aketi Jyuuzou
Block

Posts: 29
Registered: 04/05/07
Aggregation with a parent child hierarchy
Posted: Dec 6, 2007 5:35 AM
Click to report abuse...   Click to reply to this thread Reply
Hi

I have 2 tables, hierarchy and data

hierarchy has 2 columns parent and child
data contains values for the lowest rungs of the hierarchy described.

What i'd like to do is, using the hierarchy described, sum the lowest rung values up to their parent values and so on.

Quick example:

Hierarchy
child parent
1 null
2 1
3 1
4 2
5 2

Data
hiearchyid value
3 10
4 20
5 30

output required
1 60
2 50
3 10
4 20
5 30

The last 3 rows are just the 2nd table so i can just union those in but how would i work out the first two values using sql... have tried combinations of connect by, analytics (partition by etc) and rollup but to no avail...

Any help would be appreciated..

Cheers

Alex
Volder

Posts: 986
Registered: 04/14/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 6, 2007 6:24 AM   in response to: Block in response to: Block
Click to report abuse...   Click to reply to this thread Reply
no later than today in my blog :)
Block

Posts: 29
Registered: 04/05/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 6, 2007 7:07 AM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
Hi
Cheers for the reply, had a look and it's an ingenious method.
To use similar for my tables the query looks like this...

select t1.*,
(select sum(value) from
(select h.child,h.parent,nvl(d.value,0) value
from hierarchy h, data d where h.child=d.hierarchyid(+)) t2
connect by prior child = parent
start with t2.child = t1.child) summ
from
(select h.child,h.parent,nvl(d.value,0) value
from hierarchy h, data d where h.child=d.hierarchyid(+)) t1
connect by prior parent =child
start with child in(select child from data)

Which gives me extra rows of data as i'm starting with 3 children (in my example) rather than just 1 in your example. (start with id=1 is starting with the child then working your way up to 4).

Is there a simpler way using some sort of anayltic function?
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
Re: Aggregation with a parent child hierarchy
Posted: Dec 6, 2007 7:12 AM   in response to: Block in response to: Block
Click to report abuse...   Click to reply to this thread Reply
SQL> create table hierarchy (child,parent)
2 as
3 select 1, null from dual union all
4 select 2, 1 from dual union all
5 select 3, 1 from dual union all
6 select 4, 2 from dual union all
7 select 5, 2 from dual
8 /

Tabel is aangemaakt.

SQL> create table data (hierarchyid,value)
2 as
3 select 3, 10 from dual union all
4 select 4, 20 from dual union all
5 select 5, 30 from dual
6 /

Tabel is aangemaakt.

SQL> select h.child
2 , sum(connect_by_root d.value)
3 from hierarchy h
4 , data d
5 where h.child = d.hierarchyid (+)
6 connect by prior h.parent = h.child
7 group by h.child
8 order by h.child
9 /

CHILD SUM(CONNECT_BY_ROOTD.VALUE)

---------------------------
1 60
2 50
3 10
4 20
5 30

5 rijen zijn geselecteerd.

Regards,
Rob.
Block

Posts: 29
Registered: 04/05/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 6, 2007 8:23 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hi rob
Yes that's brilliant.
Connect_by_root was what i was missing..

Thanks :)

Alex
Aketi Jyuuzou

Posts: 539
Registered: 03/08/06
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 1:57 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hello Rob van Wijk.
Your solution is excellent.


Hello Volder.
I read your blog.
It is One way that We derive "sys_connect_by_path(RowIDToChar(RowID),'.') as RowIDList"
Then
We use Left Join Using "instr(RowIDList,RowIDToChar(RowID))".

my site :-)
http://oraclesqlpuzzle.hp.infoseek.co.jp/4-13.html
Volder

Posts: 986
Registered: 04/14/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 3:49 AM   in response to: Aketi Jyuuzou in response to: Aketi Jyuuzou
Click to report abuse...   Click to reply to this thread Reply
@Aketi
It is One way that We derive
"sys_connect_by_path(RowIDToChar(RowID),'.') as
RowIDList"
Then
We use Left Join Using
"instr(RowIDList,RowIDToChar(RowID))".

sorry, didn't catch what you mean.


pity, I don't know Japanese :(

@Rob

your query is good and simple :) probably it's the best alternative for the OP.
But it won't work with not a plain hierarchy, e.g.:

      1
     / \
    2   3
     \ / 
      4
     / \
    5   6
 
SQL> create table hierarchy (child,parent)
  2     as
  3     select 1, null from dual union all
  4     select 2, 1 from dual union all
  5     select 3, 1 from dual union all
  6     select 4, 3 from dual union all
  7     select 4, 2 from dual union all
  8     select 5, 4 from dual union all
  9     select 6, 4 from dual
 10  / 
 
Table created
 
SQL> 
SQL> create table data (hierarchyid,value)
  2    as
  3    select 5, 10 from dual union all
  4    select 6, 25 from dual
  5  / 
 
Table created
 
SQL> 
SQL> select h.child
  2           , sum(connect_by_root d.value)
  3        from hierarchy h
  4           , data d
  5       where h.child = d.hierarchyid (+)
  6     connect by prior h.parent = h.child
  7       group by h.child
  8       order by h.child
  9  / 
 
     CHILD SUM(CONNECT_BY_ROOTD.VALUE)
---------- ---------------------------
         1                          70
         2                          35
         3                          35
         4                          70
         5                          10
         6                          25
 
6 rows selected
 
SQL> 
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 4:02 AM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
> But it won't work with not a plain hierarchy

True, it "only" works for tree-structures. Or in other words when the child column is unique.

Regards,
Rob.
Etbin

Posts: 1,494
Registered: 09/20/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 4:16 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Tree-structure <=> all members except the root have only one parent

Regards

Etbin
Etbin

Posts: 1,494
Registered: 09/20/07
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 5:00 AM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
Interesting generalization. It seems I can master it with:
with 
the_data as
(select 1 sub,null sup,0 amount from dual union all
select 2 sub,1 sup,0 from dual union all
select 3 sub,1 sup,0 from dual union all
select 4 sub,2 sup,0 from dual union all
select 4 sub,3 sup,0 from dual union all
select 5 sub,4 sup,10 from dual union all
select 6 sub,4 sup,25 from dual
)
select distinct
sub,
case when connect_by_isleaf = 0
then (select sum(amount)
from the_data
start with sup = d.sub
connect by prior sub = sup
) + amount
else amount
end level_sum
from the_data d
start with sup is null
connect by prior sub = sup
order by sub
...
=============
SUB|LEVEL_SUM
=============
1| 70
2| 35
3| 35
4| 35
5| 10
6| 25
where each node (not just the leaves) carries it's own amount (adapted some trial version of leveled summation). There is a single data source - just a join of structure and data. It works, but I don't like that distinct in there.

Regards

Etbin

Message was edited by: Etbin
user596003
Aketi Jyuuzou

Posts: 539
Registered: 03/08/06
Re: Aggregation with a parent child hierarchy
Posted: Dec 7, 2007 2:46 PM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
sorry, didn't catch what you mean.

Oh
Sorry
I posted comment in your blog.
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums