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

T-SQL get root node in hierarchy(T-SQL 获取层次结构中的根节点)
本文介绍了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 获取层次结构中的根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)