Answer the question
In order to leave comments, you need to log in
How to find sum of depths of subtrees using SQL query?
Good afternoon.
Having trouble with a PostgreSQL tutorial problem on subtree sizing.
There is a table like this:
Keyword(id INT PRIMARY KEY, value TEXT, parent_id INT REFERENCES Keyword DEFAULT NULL);
Keyword
id value parent_id
-------------------------
0 '1' NULL
1 '2' 0
2 '3' 0
3 '4' 1
4 '5' 1
id size
-------------
0 5
1 3
2 1
3 1
4 1
WITH RECURSIVE tree AS (
SELECT id, parent_id, array[id] AS path, 0 AS level FROM Keyword
UNION ALL
SELECT Child.id, Parent.id, array_append(path, Child.id) AS parent_id, Parent.level + 1 AS level
FROM Keyword Child JOIN tree Parent ON Child.parent_id = Parent.id
)
SELECT * FROM tree
Answer the question
In order to leave comments, you need to log in
The result is a recursive query that prints the sum of subtree depths for each tree node.
On each pass, we build a table containing the node id and the path from the parent. After that, for each vertex, we count how many paths contain the id of the vertex as a parent.
WITH RECURSIVE tree_depth as (
SELECT id, array[id] AS parents, 0 AS level
FROM Keyword
UNION ALL
SELECT C.id, P.parents || C.id, P.level + 1 as level FROM Keyword C
JOIN tree_depth P ON P.id = C.parent_id
)
Select parents[1], COUNT(*) from tree_depth group by parents[1] order by parents[1];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question