Skip to content
Advertisement

How to get the cumulative sum of children up its parents?

So I have written a query to get the cumulative sum of children but I think partition sum has error as its totalling for the parent that is not part of the children.

My fiddle is http://sqlfiddle.com/#!15/88828/1 I have dont a running total but thats wrong. I want the siblings total to a child and child total back to its parent.So basically cumulative total of a child up the tree.

expected output

enter image description here

Advertisement

Answer

To get totals for tree nodes you need to generate hierarchy tree for every node in a subquery like this

If you really need all those “decoration” columns that you generate in your query for each tree node than you can combine your recursive CTE hierarchy with subquery for totals calculation like this

You can check a working demo here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement