递归查询按查找顺序排序

时间:2019-05-09 16:04:44

标签: sql sql-server tsql recursive-query

我正在构建一个查询,其中我需要按照查询找到值的顺序返回结果。

查询在这里

DECLARE @Query TABLE
(
    [ParentID] VARCHAR(50) NULL,
    [ChildID] VARCHAR(50) NOT NULL,
    [Action] VARCHAR(max) NOT NULL
);

WITH q
AS
(
    SELECT DISTINCT M1.[ParentID], M1.[ChildID], M1.[Action]
    FROM [dbo].[xNodes] M1
        LEFT JOIN [dbo].[xNodes] M2 ON M1.[ParentID] = M2.[ChildID]
    WHERE M1.[ChildID] = 'PCMN014'
    UNION ALL
    SELECT M.[ParentID], M.[ChildID], M.[Action]
    FROM q
        INNER JOIN [dbo].[xNodes] M ON q.[ParentID] = M.[ChildID]
)
INSERT INTO @Query ([ParentID], [ChildID], [Action])
SELECT DISTINCT * FROM q;

SELECT DISTINCT * FROM @Query

SELECT * FROM @Query q WHERE [ParentID] IS NULL UNION ALL 
SELECT * FROM @Query q WHERE [ParentID] = 'IDS' UNION ALL 
SELECT * FROM @Query q WHERE [ParentID] = 'EFL' UNION ALL 
SELECT * FROM @Query q WHERE [ParentID] = 'PEFL001' UNION ALL 
SELECT * FROM @Query q WHERE [ParentID] = 'PCMN010'

最后一个丑陋的联合显示了我想要它们的顺序。下表表示我从查询中得到的信息。

***ParentID, ChildID, Action***
NULL,       IDS,        Launch IDS
EFL,        PEFL001,    [Click] Case Management > Case Review Workbasket
IDS,        EFL,        Log into IDS
PCMN010,    PCMN014,    [Click] Title Search arrow beside Title
PEFL001,    PCMN010,    [Click] User Search arrow beside User ID
PEFL001,    PCMN010,    [Click] User Search arrow under File Location - Person

以下是我的期望。请注意,第二行的ParentID是第一行的ChildID,依此类推。

***ParentID, ChildID, Action***
NULL,       IDS,        Launch IDS
IDS,        EFL,        Log into IDS
EFL,        PEFL001,    [Click] Case Management > Case Review Workbasket
PEFL001,    PCMN010,    [Click] User Search arrow beside User ID
PEFL001,    PCMN010,    [Click] User Search arrow under File Location - Person
PCMN010,    PCMN014,    [Click] Title Search arrow beside Title

1 个答案:

答案 0 :(得分:0)

让我们添加一个Seq列,这将有助于获得所需的顺序。

DECLARE @Query TABLE
(
    [ParentID] VARCHAR(50) NULL,
    [ChildID] VARCHAR(50) NOT NULL,
    [Action] VARCHAR(max) NOT NULL,
    [Seq] int
);

WITH q
AS
(
    SELECT DISTINCT M1.[ParentID], M1.[ChildID], M1.[Action],1 [Seq]
    FROM [dbo].[xNodes] M1
        LEFT JOIN [dbo].[xNodes] M2 ON M1.[ParentID] = M2.[ChildID]
    WHERE M1.[ChildID] = 'PCMN014'
    UNION ALL
    SELECT M.[ParentID], M.[ChildID], M.[Action],q.[Seq]+1 [Seq]
    FROM q
        INNER JOIN [dbo].[xNodes] M ON q.[ParentID] = M.[ChildID]
)
INSERT INTO @Query ([ParentID], [ChildID], [Action],[Seq])
SELECT DISTINCT * FROM q 

SELECT [ParentID], [ChildID], [Action] FROM @Query order by [Seq] desc

输出将是:-

ParentID    ChildID            Action
========    =======            =====
NULL        IDS                Launch IDS
IDS         EFL                Log into IDS
EFL         PEFL001            [Click] Case Management > Case Review Workbasket
PEFL001     PCMN010            [Click] User Search arrow beside User ID
PEFL001     PCMN010            [Click] User Search arrow under File Location - Person
PCMN010     PCMN014            [Click] Title Search arrow beside Title