T-SQL 获取层次结构中的根节点

2022-10-21数据库问题
4

本文介绍了T-SQL 获取层次结构中的根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我有两个这样结构的表:

So I have two tables structured like so:

CREATE TABLE #nodes(node int NOT NULL);
ALTER TABLE #nodes ADD CONSTRAINT PK_nodes PRIMARY KEY CLUSTERED (node);

CREATE TABLE #arcs(child_node int NOT NULL, parent_node int NOT NULL);
ALTER TABLE #arcs ADD CONSTRAINT PK_arcs PRIMARY KEY CLUSTERED (child_node, parent_node);

INSERT INTO #nodes(node)
VALUES (1), (2), (3), (4), (5), (6), (7);

INSERT INTO #arcs(child_node, parent_node)
VALUES (2, 3), (3, 4), (2, 6), (6, 7);

如果我有两个节点,比如说 1 和 2.我想要一个它们的根节点的列表.在这种情况下,它将是 1、4 和 7.我如何编写查询来获取该信息?

If I have two nodes, lets say 1 and 2. I want a list of their root nodes. In this case it would be 1, 4, and 7. How can I write a query to get me that information ?

我尝试编写它,但遇到了一个问题,即由于某种未知原因,我无法在 CTE 的递归部分使用 LEFT 连接.如果允许我执行 LEFT JOIN,这里是可行的查询.

I took a stab at writing it but ran into the issue that I can't use a LEFT join in the recursive part of a CTE for some unknown reason. Here is the query that would work if I was allowed to do a LEFT JOIN.

WITH root_nodes
AS (
    -- Grab all the leaf nodes I care about and their parent
    SELECT n.node as child_node, a.parent_node
    FROM #nodes n
    LEFT JOIN #arcs a
      ON n.node = a.child_node
    WHERE n.node IN (1, 2)

    UNION ALL

    -- Grab all the parent nodes
    SELECT rn.parent_node as child_node, a.parent_node
    FROM root_nodes rn
    LEFT JOIN #arcs a -- <-- LEFT JOINS are Illegal for some reason :(
      ON rn.parent_node = a.child_node
    WHERE rn.parent_node IS NOT NULL
)
SELECT DISTINCT rn.child_node as root_node
FROM root_nodes rn
WHERE rn.parent_node IS NULL

有什么方法可以重组查询以获得我想要的?我无法重组数据,我真的希望远离临时表或不得不做任何昂贵的事情.

Is there a way I can restructure the query to get what I want ? I can't restructure the data and I would really prefer to stay away from temporary tables or having to do anything expensive.

谢谢,劳尔

推荐答案

将 LEFT JOIN 移出 CTE 怎么样?

How about moving the LEFT JOIN out of the CTE?

WITH root_nodes
AS (
    -- Grab all the leaf nodes I care about
    SELECT NULL as child_node, n.node as parent_node
    FROM #nodes n
    WHERE n.node IN (1, 2)

    UNION ALL

    -- Grab all the parent nodes
    SELECT rn.parent_node as child_node, a.parent_node
    FROM root_nodes rn
        JOIN #arcs a
      ON rn.parent_node = a.child_node
)
SELECT DISTINCT rn.parent_node AS root_node
FROM root_nodes rn
    LEFT JOIN #arcs a
  ON rn.parent_node = a.child_node
WHERE a.parent_node IS NULL

结果集为 1、4、7.

The result set is 1, 4, 7.

这篇关于T-SQL 获取层次结构中的根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
T-SQL

相关推荐

如何像在 T-SQL 中一样在 PL/SQL 中声明和使用变量?
How do I declare and use variables in PL/SQL like I do in T-SQL?(如何像在 T-SQL 中一样在 PL/SQL 中声明和使用变量?)...
2023-11-03 数据库问题
1

用于查找冗余索引的 T-SQL
T-SQL for finding Redundant Indexes(用于查找冗余索引的 T-SQL)...
2023-10-25 数据库问题
4

在 T-SQL 中透视数据
Pivot data in T-SQL(在 T-SQL 中透视数据)...
2023-10-10 数据库问题
3

仅加入“最新"用t-sql记录
Join to only the quot;latestquot; record with t-sql(仅加入“最新用t-sql记录)...
2023-10-09 数据库问题
3

T-SQL 子查询 Max(Date) 和 Joins
T-SQL Subquery Max(Date) and Joins(T-SQL 子查询 Max(Date) 和 Joins)...
2023-10-08 数据库问题
0

如何通过使用 T-SQL 将两个整数值相除来获得浮点结果?
How to get a float result by dividing two integer values using T-SQL?(如何通过使用 T-SQL 将两个整数值相除来获得浮点结果?)...
2023-07-17 数据库问题
1