SQL Server 2005 中的分层查询

发布于 2024-08-06 03:53:02 字数 361 浏览 6 评论 0原文

我在 SQL Server 2005 的表内有一个数据结构,表示相关对象链。每个对象都可以在多个步骤中进行替换。我想执行一个查询,返回替换链中的所有对象和每个对象的叶子。

数据:

id  replacement
1   null
2   3
3   null
4   5
5   6
6   null

结果应该是:

id  replacement
1   null
2   3
3   null
4   6
5   6
6   null

我相信递归 CTE 将是一个好方法,但我无法理解它。问题的一个限制是我无法更改数据结构,因为数据库不在我的控制范围内。

I have a data structure inside a table in SQL Server 2005 representing a chain of related objects. Each object can have replacements in many steps. I want to perform a query that returns all objects and each object's leaf in the replacement chain.

The data:

id  replacement
1   null
2   3
3   null
4   5
5   6
6   null

The result should be:

id  replacement
1   null
2   3
3   null
4   6
5   6
6   null

I believe that a recursive CTE would be a good way to go, but I can't wrap my head around it. A constraints to the problem is that I can't change the data structure, since the database is not in my control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-08-13 03:53:02

看看这个

DECLARE @Table TABLE(
        ID INT,
        ReplacementID INT
)

INSERT INTO @Table (ID,ReplacementID) SELECT 1, NULL
INSERT INTO @Table (ID,ReplacementID) SELECT 2, 3
INSERT INTO @Table (ID,ReplacementID) SELECT 3, NULL
INSERT INTO @Table (ID,ReplacementID) SELECT 4, 5
INSERT INTO @Table (ID,ReplacementID) SELECT 5, 6
INSERT INTO @Table (ID,ReplacementID) SELECT 6, NULL

INSERT INTO @Table (ID,ReplacementID) SELECT 7, 8
INSERT INTO @Table (ID,ReplacementID) SELECT 8, 9
INSERT INTO @Table (ID,ReplacementID) SELECT 9, 10
INSERT INTO @Table (ID,ReplacementID) SELECT 10, NULL

SELECT * FROM @Table

;WITH repl AS (
    SELECT  *, 1 AS Depth
    FROM    @Table t
    UNION   ALL
    SELECT  r.ID,
            t.ReplacementID,
            r.Depth + 1
    FROM    repl r INNER JOIN
            @Table t ON r.ReplacementID = t.ID
    WHERE   t.ReplacementID IS NOT NULL
)
SELECT  repl.ID,
        repl.ReplacementID
FROM    (
            SELECT  ID,
                    MAX(Depth) Depth
            FROM    repl
            GROUP BY ID
        ) Depths INNER JOIN
        repl    ON  Depths.ID = repl.ID
                AND Depths.Depth = repl.Depth
ORDER BY 1

Have a look at this

DECLARE @Table TABLE(
        ID INT,
        ReplacementID INT
)

INSERT INTO @Table (ID,ReplacementID) SELECT 1, NULL
INSERT INTO @Table (ID,ReplacementID) SELECT 2, 3
INSERT INTO @Table (ID,ReplacementID) SELECT 3, NULL
INSERT INTO @Table (ID,ReplacementID) SELECT 4, 5
INSERT INTO @Table (ID,ReplacementID) SELECT 5, 6
INSERT INTO @Table (ID,ReplacementID) SELECT 6, NULL

INSERT INTO @Table (ID,ReplacementID) SELECT 7, 8
INSERT INTO @Table (ID,ReplacementID) SELECT 8, 9
INSERT INTO @Table (ID,ReplacementID) SELECT 9, 10
INSERT INTO @Table (ID,ReplacementID) SELECT 10, NULL

SELECT * FROM @Table

;WITH repl AS (
    SELECT  *, 1 AS Depth
    FROM    @Table t
    UNION   ALL
    SELECT  r.ID,
            t.ReplacementID,
            r.Depth + 1
    FROM    repl r INNER JOIN
            @Table t ON r.ReplacementID = t.ID
    WHERE   t.ReplacementID IS NOT NULL
)
SELECT  repl.ID,
        repl.ReplacementID
FROM    (
            SELECT  ID,
                    MAX(Depth) Depth
            FROM    repl
            GROUP BY ID
        ) Depths INNER JOIN
        repl    ON  Depths.ID = repl.ID
                AND Depths.Depth = repl.Depth
ORDER BY 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文