次のようなツリー構造のテーブルがあります。
Id Desc ParentID
===
A DescrA NULL
B DescrB A
C DescrC A
D DescrD C
E DescrE C
F DescrF E
次のような特定の ID の子孫 (下位子孫を含む) の数を返すクエリが必要です。
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
CTEで「簡単に」作成できることを見てきましたが、その要点を理解できませんでした...