Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

cumulative sum

user13117585Apr 6 2024

hi all,

Could anyone suggest on best approach for this kind of cumulative sums and counts?

Suppose we have a tree structure data and we want to start from any idea of the tree and get count/sums for the whole branch?

with data as 
(
-- one root node
select 1 AS id, NULL as pid, NULL as s FROM dual UNION ALL
-- one root node with 2 node
select 2 AS id, NULL as pid, NULL as s FROM dual UNION ALL
select 3 AS id, 2    as pid,    3 as s FROM dual UNION ALL
select 4 AS id, 2    as pid,    4 as s FROM dual
)
select id, pid, level, s
from data
start with id = 4
connect by prior pid = id;

If I start with id 4, I have one sibling and one parent.

What is the best way to have the whole branch (here id 2 and 4) with proper counts (here id 2 has two nodes count should be 2) and sum of size (here sum for id 2 must be 7 and for leaves, the sum is their own size).

What would be the best approach to do this kind of calculations?

thank you

This post has been answered by Frank Kulash on Apr 6 2024
Jump to Answer
Comments
Post Details
Added on Apr 6 2024
10 comments
208 views