gpt4 book ai didi

sql-server - 如何对父节点的子节点进行唯一编号

转载 作者:行者123 更新时间:2023-12-02 21:56:41 25 4
gpt4 key购买 nike

我有一张像这样的 table

ID               Node                  ParentID
1 A 0
2 B 1
3 C 1
4 D 2
5 E 2
6 F 3
7 G 3
8 H 3
9 I 4
10 J 4
11 K 10
12 L 11

我需要一个查询来生成一个“位置”字段,其中包含节点在其父节点中出现的顺序。下面的例子

ID               Node                  ParentID                  Positon
1 A 0 0
2 B 1 0
3 C 1 1
4 D 2 0
5 E 2 1
6 F 3 0
7 G 3 1
8 H 3 2
9 I 4 0
10 J 4 1
11 K 10 0
12 L 11 0

最佳答案

select  *
, row_number() over (partition by ParentID order by ID) - 1 as Position
from YourTable

作为更新查询:

update  yt
set Position = nr
from (
select *
, row_number() over (partition by ParentID order by ID) - 1 rn
from YourTable
) yt

关于sql-server - 如何对父节点的子节点进行唯一编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17653214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com