There is a table with an usual "child-to-parent" structure, where each node has weight.
TREE_TABLE
----------------------------------------
id | parent_id | name | weight
----------------------------------------
1 | NULL | N1 | 51
2 | 1 | N12 | 62
3 | 1 | N13 | 73
4 | 2 | N124 | 84
5 | 2 | N125 | 95
// for convenience the "name" column shows path from a node to the root node
How to build a SQL query that results in a set of rows where each row represents grouping per particular node and its children?
Please, use any SQL dialect on your choice. Just need a general idea or a prototype of the solution.
To solve this problem, I am experimenting with GROUPING SETS and ROLLUP report, but can't figure out the way of handling "dynamic" number of grouping levels.
Example of expected query result:
RESULT
--------------------------
name | summary_weight
--------------------------
N1 | (51+62+73+84+95)
N12 | (62+84+95)
N124 | (84)
N125 | (95)
N13 | (73)