开发者

how do I get a list of all descendant nodes related to its parent node in rows in tsql?

开发者 https://www.devze.com 2023-03-13 06:37 出处:网络
as a sample, my table (table name: hier) looks like this: parentID childID -------- ------- 01 12 13 25 28 34

as a sample, my table (table name: hier) looks like this:

parentID childID
-------- -------
0        1
1        2
1        3
2        5
2        8
3        4
3        6
3        7
4        9

and I want it to output this:

parentID RelatedID
-------- ---------
0        1开发者_运维问答
0        2
0        3
0        4
0        5
0        6
0        7
0        8
0        9
1        2
1        3
1        4
1        5
1        6
1        7
1        8
1        9
2        5
2        8
3        4
3        6
3        7
3        9
4        9


With cte(p, d)
As
(
    Select a.parentID, b.childID From hier a inner join hier b on a.childID=b.parentID
)
Select * From cte Union Select * From hier
0

精彩评论

暂无评论...
验证码 换一张
取 消