We have a table with tree structure like this:
Id Desc ParentID
===
A DescrA NULL
B DescrB A
C DescrC A
D DescrD C
E DescrE C
F DescrF E
We need a query that returns the number of descendants (including subdescendants) of a specific ID, something like:
select count(descendants) from Tablex where id='A' --result = 5
select count(descendants) from Tablex where id='B' --result = 0
select count(descendants) from Tablex where id='C' --result = 3
select count(descendants) from Tablex where id='E' --result = 1
We have seen it would be made ‘easily’ with CTE but couldnt get the gist of it…
1 Answer