PostgreSQL
0
Вклад в тег
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];