I have one table containing ID, Amountand Parent_ID. (This is actually a little more complicated, but these are all important columns for this example). Basically, what I want to do is group and sum the Sum column by the lowest common parent (i.e. those with a parent identifier of NULL).
ID Amount Parent_ID
1 100 NULL
2 150 1
3 50 1
4 75 3
5 25 4
6 125 NULL
7 50 6
8 50 7
9 100 8
Expected results:
ID SUM
1 400
6 325
As you can see, it only reconfigures the two entries, those who do not have a parent, which means that they are top-level elements. The Sum column is the sum of all its children Amountrecursively, therefore ID= 1 is the sum of 1,2,3,4 and 5. and ID= 6 is the sum of 6,7, 8 and 9.
source
share