获取所有帖子及其下属帖子

时间:2017-02-22 11:29:59

标签: sql-server tsql sql-server-2016

我希望你能帮助我。我正在使用SQL Server 2016,我正在尝试生成一些代码来返回所有帖子及其所有下属帖子。

到目前为止,以下代码是:

;WITH CTE AS
(
SELECT
    P.[Post Number],
    P.[Authoriser Post Number],
    P.[Post Name]
FROM
    Structure.Post P
WHERE
    P.[Authoriser Post Number] IS NULL
UNION ALL
SELECT
    C.[Post Number],
    C.[Authoriser Post Number],
    C.[Post Name]
FROM
    Structure.Post C
JOIN
    CTE P ON C.[Authoriser Post Number] = P.[Post Number]
) SELECT * FROM CTE

结果:

[Post Number]   [Authoriser Post Number]    [Post Name]
85              NULL                        CEO
86              85                          Development Director
87              85                          Sales Director
88              85                          HR Director
89              88                          HR Manager
90              89                          HR Officer
91              89                          Recruitment Officer
92              89                          HR Assistant
93              87                          Sales Manager
94              87                          Sales Manager
97              94                          Salesman
98              94                          Sales Support Officer
95              93                          Salesman
96              93                          Sales Support Officer
99              86                          Web Design Manager
100             86                          SQL Manager
104             100                         Project Manager
105             100                         SQL Developer
101             99                          Web Developer
102             99                          C# Developer
103             99                          Project Manager

我想要的是邮政“CEO”显示所有邮寄号码,然后邮政“开发总监”显示层次结构等下面所有帖子的所有帖子号码。

我将不胜感激。

3 个答案:

答案 0 :(得分:0)

你很好。 现在使用您的CTE,您可以为每个Post下属创建SUBSELECT并连接它们,例如。他们FOR XML

SELECT
   P.[Post Number],
   P.[Authoriser Post Number],
   P.[Post Name],
   (    
     SELECT 
            cte2.[Post name] + ', ' AS [text()] 
     FROM CTE cte2
     WHERE cte1.[Post Number] = cte2.[Post Number]
     ORDER BY cte2.[Post name] -- This is optional
     FOR XML PATH ('')
   ) [Subordinates]
FROM CTE cte1

答案 1 :(得分:0)

我相信我找到了答案。这正是我所需要的。典型的是我在发布问题后很快得到答案。

;WITH CTE AS
   (
   SELECT
          P.[Post Number],
          P.[Authoriser Post Number],
          P.[Post Name]
   FROM
          Structure.Post P
   UNION ALL
   SELECT
          C.[Post Number],
          P.[Authoriser Post Number],
          C.[Post Name]
   FROM
          Structure.Post C
   JOIN
          CTE P ON C.[Authoriser Post Number] = P.[Post Number]
   ) SELECT DISTINCT * FROM CTE WHERE [Authoriser Post Number] IS NOT NULLORDER BY [Authoriser Post Number]

感谢所有花时间为我看这个的人。

答案 2 :(得分:0)

这是一项有趣的练习。你想看看我的解决方案吗? 您可以获取层次结构列表或某些分支列表。它只需要改变WHERE子句中的条件。

DECLARE @Structure TABLE
(
    [Post Number]   INT,
    [Authoriser Post Number]    INT,
    [Post Name] VARCHAR(32)
)

INSERT @Structure
VALUES
(85, NULL, 'CEO'),
(86, 85, 'Development Director'),
(87, 85, 'Sales Director'),
(88, 85, 'HR Director'),
(89, 88, 'HR Manager'),
(90, 89, 'HR Officer'),
(91, 89, 'Recruitment Officer'),
(92, 89, 'HR Assistant'),
(93, 87, 'Sales Manager'),
(94, 87, 'Sales Manager'),
(97, 94, 'Salesman'),
(98, 94, 'Sales Support Officer'),
(95, 93, 'Salesman'),
(96, 93, 'Sales Support Officer'),
(99, 86, 'Web Design Manager'),
(100, 86, 'SQL Manager'),
(104, 100, 'Project Manager'),
(105, 100, 'SQL Developer'),
(101, 99, 'Web Developer'),
(102, 99, 'C# Developer'),
(103, 99, 'Project Manager')

;WITH cte
AS
(
    SELECT  [Post Number], [Authoriser Post Number], [Post Name], 1 AS Num, CAST('/' + CAST([Post Number] AS VARCHAR) + '/' AS VARCHAR(4000)) AS Hierarchy
    from @Structure
    WHERE [Authoriser Post Number] IS NULL
    UNION ALL
    SELECT s.[Post Number], s.[Authoriser Post Number], s.[Post Name],
        p.Num + 1, CAST(Hierarchy + CAST(s.[Post Number] AS VARCHAR) + '/' AS VARCHAR(4000))
    from @Structure s
        JOIN cte p ON p.[Post Number] = s.[Authoriser Post Number]
)

SELECT c2.[Post Number], c2.[Authoriser Post Number], SPACE(c2.NUM*5) + c2.[Post Name] AS [Post Name]
FROM cte c1
    JOIN cte c2 ON (c2.Num > c1.Num AND c2.Hierarchy LIKE '%/' + CAST(c1.[Post Number] AS VARCHAR) + '/%') OR c2.[Post Number] = c1.[Post Number]
WHERE c1.[Post Name] = 'CEO'
ORDER BY c2.Hierarchy