如何创建 SQL Server 2005 CTE 以返回具有多个父项的子项的父子记录
我正在 SQL Server 中试验 CTE,但在让以下场景发挥作用方面已经陷入了死胡同。 我有一个与此类似的层次结构表:
Node(ID:439)
Node(ID:123)
Node(ID:900)
Node(ID:56)
Node(ID:900)
预期结果:
NodeID ParentNodeID
439 0
123 439
900 123
56 439
900 56
所以基本上我们有一个父子层次结构表,但有一个细微的差别。 每个孩子可能有多个父母。 我研究了许多关于创建返回父子记录的 CTE 的博客文章和 StackOverflow 帖子,但它们不会返回子级的所有父级,而只是返回找到的第一个父级。
这是我尝试过的一个示例 CTE:(
WITH Hierarchy(NodeID, ParentNodeID)
AS
(
SELECT
T1.NodeID,
T1.ParentNodeID
FROM
ParentChildTable T1
WHERE
T1.NodeID = 439
UNION ALL
SELECT
T1.NodeID,
T1.ParentNodeID
FROM
Heirarchy T1
INNER JOIN Heirarchy TH ON TH.NodeID = T1.ParentNodeID
)
注意:出于隐私目的,上述 CTE 中的表和列的名称已从原始名称更改。)
上述 CTE 工作正常,它找到从 ID 开始的所有父子记录:439,但它只找到项目 ID:900 的一个父项,即使它有两个父项。
有人可以告诉我这是否可以使用 CTE 实现,或者是否有其他 SQL 方法可以做到这一点?
干杯。 贾斯。
I'm experimenting with CTE's in SQL Server but have reached a dead end with getting the following scenario to work. I have a hierarchy table similar to this:
Node(ID:439)
Node(ID:123)
Node(ID:900)
Node(ID:56)
Node(ID:900)
Expected results:
NodeID ParentNodeID
439 0
123 439
900 123
56 439
900 56
So basically we have a parent-child hierarchy table, with one subtle difference. Each child could potentially have more then one parent. I have researched many blog articles, and StackOverflow posts, about creating CTE's that return parent-child records, but they don't return all of the parents for the children, just the first one that it finds.
Here's an example CTE that I tried:
WITH Hierarchy(NodeID, ParentNodeID)
AS
(
SELECT
T1.NodeID,
T1.ParentNodeID
FROM
ParentChildTable T1
WHERE
T1.NodeID = 439
UNION ALL
SELECT
T1.NodeID,
T1.ParentNodeID
FROM
Heirarchy T1
INNER JOIN Heirarchy TH ON TH.NodeID = T1.ParentNodeID
)
(Note: The names of the tables and columns in the above CTE have been changed from the orginal for privacy purposes.)
The above CTE works fine, it finds all the parent-child records starting from ID:439, but it only finds one parent for item ID:900, even though it has two parents.
Could someone let me know if this is possible using CTE's, or is there another SQL way to do this?
Cheers.
Jas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦我更正了 CTE 中的语法错误,这对我来说似乎工作正常:
返回结果:
This appears to work OK for me, once I corrected the syntax error in your CTE:
Returns the result:
这是我用来寻找解决方案的链接..
http://wiki.lessthandot.com /index.php/Using_Common_Table_Expressions_for_Parent-Child_Relationships
编辑-@Pshimo-谢谢。 这是链接中的指南。
对于 SQL 2005,这只是一个梦想。 假设您有以下示例数据:
并且您想要获取 1 的所有子行(因此 ItemId 5、16、27)
,并且如果您想要包含父行:
如果您愿意,您也可以选择层次结构中的深度类似的事情:
正如您所看到的,您在这里所做的就是首先选择初始记录集,其中包含父 ID 参数的所有子行。 然后,您可以联合到另一个连接到 CTE 本身的查询,以获取子项的子项(及其孙子项,依此类推,直到到达最后一个后代行。值得注意的是,默认递归限制为 100,因此请支付使用这些时请注意层次结构的深度,您可以使用 OPTION (MAXRECURSION) 更改递归限制。
This is the link I used to find a solution..
http://wiki.lessthandot.com/index.php/Using_Common_Table_Expressions_for_Parent-Child_Relationships
EDIT - @Pshimo - thanks. Heres the guide form the link.
With SQL 2005 though it is a dream. Say you have this sample data:
And you want to get all child rows for 1 (so ItemId 5, 16, 27)
and if you want to include the parent:
You can select depth in the hierarchy as well, if you're into that kind of thing:
As you can see what you're doing here is first selecting your initial recordset, which contains all child rows for your parent ID parameter. You can then union to another query that joins to the CTE itself, to get the children's children (and their grandchildren, and so forth until you reach the last descendant row. Its' important to note that the default recursion limit is 100, so pay attention to the depth of your hierarchy when using these. You can change the recursion limit using the OPTION (MAXRECURSION)