SQL查找主管所有员工的报告

时间:2020-02-04 19:06:28

标签: sql-server tsql

我发现了许多创建员工报告结构层次结构的示例。但是,我想进一步介绍一下,为每位员工找到他们要汇报的高管,然后由他们直接向首席执行官汇报。给定以下数据:

ID          FullName                                           HRLevel    SupervisorID
----------- -------------------------------------------------- ---------- ------------
1           Pam Beesly                                         CEO        NULL
2           Angela Martin                                      SVP        1
3           Kelly Kapoor                                       SVP        1
4           Meredith Palmer                                    SVP        1
5           Phyllis Vance                                      AVP        3
6           Jan Levinson                                       AVP        4
7           Erin Hannon                                        Associate  5
8           Karen Filippelli                                   Intern     5

我希望列出所有HRLevel为SVP或以下的员工,以显示他们所报告的SVP级员工。因此它可以是他们的直接主管,也可以是他们之上的4级。另外,如果某个员工直接向首席执行官报告,我希望该员工被列为DepartmentHead。

这是我到目前为止所拥有的:

;WITH cte_employees (ID, FullName, HRLevel, SupervisorID, SupervisorName) AS
(
    SELECT ID
         , FullName
         , HRLevel
         , SupervisorID
         , CONVERT(VARCHAR(50), NULL) AS [SupervisorName]
    FROM   #Employees
    WHERE  HRLevel = 'SVP'
    UNION ALL
    SELECT     e.ID
             , e.FullName
             , e.HRLevel
             , e.SupervisorID
             , c.FullName AS [SupervisorName]
    FROM       #Employees    e 
    INNER JOIN cte_employees c
    ON e.SupervisorID = c.ID
)
SELECT c2.ID
     , c2.FullName
     , c2.HRLevel
     , c2.SupervisorID
     , COALESCE(c2.SupervisorName, c2.FullName) AS [DepartmentHead]
FROM   cte_employees c2;

由哪个直接上司而不是高级副总裁负责:

ID          FullName                                           HRLevel    SupervisorID DepartmentHead
----------- -------------------------------------------------- ---------- ------------ --------------------------------------------------
2           Angela Martin                                      SVP        1            Angela Martin
3           Kelly Kapoor                                       SVP        1            Kelly Kapoor
4           Meredith Palmer                                    SVP        1            Meredith Palmer
5           Phyllis Vance                                      AVP        3            Kelly Kapoor
6           Jan Levinson                                       AVP        4            Meredith Palmer
7           Erin Hannon                                        Associate  5            Phyllis Vance
8           Karen Filippelli                                   Intern     5            Phyllis Vance

我得到的和我想要的唯一的区别是,员工ID 7和8应该有Kelly Kapoor的SVP DepartmentHeader。

这是我的dbfiddle,它正在起作用。

1 个答案:

答案 0 :(得分:1)

您正在关闭:

IF OBJECT_ID('tempdb.dbo.#Employees ', 'U') IS NOT NULL DROP TABLE #Employees;

CREATE TABLE #Employees(ID INT,
FullName VARCHAR(50),
HRLevel VARCHAR(10),
SupervisorID INT)

INSERT INTO #Employees VALUES (1, 'Pam Beesly', 'CEO', NULL)
INSERT INTO #Employees VALUES (2, 'Angela Martin', 'SVP', 1)
INSERT INTO #Employees VALUES (3, 'Kelly Kapoor', 'SVP', 1)
INSERT INTO #Employees VALUES (4, 'Meredith Palmer', 'SVP', 1)
INSERT INTO #Employees VALUES (5, 'Phyllis Vance', 'AVP', 3)
INSERT INTO #Employees VALUES (6, 'Jan Levinson', 'AVP', 4)
INSERT INTO #Employees VALUES (7, 'Erin Hannon', 'Associate', 5)
INSERT INTO #Employees VALUES (8, 'Karen Filippelli', 'Intern', 5)



;WITH cte_employees (ID, FullName, HRLevel, SupervisorID, SupervisorName) AS
(
    SELECT ID
         , FullName
         , HRLevel
         , SupervisorID
         , FullName AS [SupervisorName]
    FROM   #Employees
    WHERE  HRLevel = 'SVP'
    UNION ALL
    SELECT     e.ID
             , e.FullName
             , e.HRLevel
             , e.SupervisorID
             , c.SupervisorName AS [SupervisorName]
    FROM       #Employees    e 
    INNER JOIN cte_employees c
    ON e.SupervisorID = c.ID
)
SELECT c2.ID
     , c2.FullName
     , c2.HRLevel
     , c2.SupervisorID
     , c2.SupervisorName AS [DepartmentHead]
FROM   cte_employees c2;