我们如何处理嵌套层次结构

问题描述 投票:0回答:0

我有以下数据,需要填充只有主父 ID 的新列,即 6

Child ID     Parent ID   Master Parent ID
1             2           6
2             3           6
3             4           6
4             5           6
5             6           6
6             6           6

我尝试使用以下更新查询来更新主父 ID

UPDATE table dummy t1
SET t1.Master_Parent_ID = t2.Parent_ID
FROM table dummy t2    
WHERE t1.Parent_ID = t2.Child_id

但这给了我以下结果

Child ID     Parent ID   Master Parent ID
1             2           3
2             3           4
3             4           5
4             5           6
5             6           6
6             6           6

有人可以指导如何达到预期的结果

sql hierarchy hierarchical-data
© www.soinside.com 2019 - 2024. All rights reserved.