【问题标题】:Get the superior of the employee. Then get again the superior of the superior of the employee. And so on得到员工的上级。然后再次得到员工上级的上级。等等
【发布时间】:2021-08-25 02:15:42
【问题描述】:

我有一个雇员表,其中有一列 Employee_ID 和 SuperiorID。我想获得特定员工的所有层级上司。比如Employee_ID-1000000的上级是1000001,1000002,1000003,1000004,1000005

+--------------------------+
| Employee_ID | SuperiorID |
+--------------------------+
|   1000000   |    1000001 |
|   1000001   |    1000002 |
|   1000002   |    1000003 |
|   1000003   |    1000004 |
|   1000004   |    1000005 |
+--------------------------+

Employee_ID - 1000005 是公司的总裁。

我现在有一个问题:

DECLARE @EmployeeNo AS INT = 1000000
SELECT Superior_ID from EmployeeTable WHERE Employee_ID  = @EmployeeNo
UNION
SELECT Superior_ID from EmployeeTable WHERE Employee_ID  = (select Superior_ID from EmployeeTable WHERE Employee_ID  = @EmployeeNo)
UNION
SELECT Superior_ID from EmpoyeeTable WHERE Employee_ID = ( SELECT Superior_ID from EmployeeTable WHERE Employee_ID  = (select Superior_ID from EmployeeTable WHERE Employee_ID  = @EmployeeNo))
 

有没有更好的方法来做这个?

感谢您的帮助。

【问题讨论】:

  • 递归 CTE??

标签: sql sql-server tsql sql-server-2012


【解决方案1】:

我们可以在这里使用递归分层查询。下面的策略是建立完整的层次结构,从员工1000000 开始向上指挥链。递归在员工1000004 处停止,其上级没有进一步的记录。在下面的递归 CTE 中,我跟踪一个计数器,随着层次结构的上升,每一步都会递增。然后,我们使用TOP 技巧仅选择排名最高的记录。

WITH cte (EmployeeID, SuperiorID, n) AS (
    SELECT Employee_ID, SuperiorID, 1
    FROM EmployeeTable
    WHERE Employee_ID = 1000000
    UNION ALL
    SELECT e.Employee_ID, e.SuperiorID, n+1
    FROM cte t
    INNER JOIN EmployeeTable e
        ON t.SuperiorID = e.Employee_ID
)

SELECT TOP 1 *
FROM cte
ORDER BY n DESC;

Demo

【讨论】:

  • 很抱歉,在我的询问会以上级的身份送达总统之前,我没有将其包含在内。
  • @Rigel1121 不清楚你想要什么。在原始问题中包含您想要的确切输出。很可能,我只需要对我的答案进行一行更改。
【解决方案2】:

是的,你需要recursive cte

DECLARE @EmployeeNo AS INT = 1000000;
with cte as (
  select Employee_ID, SuperiorID
  from EmployeeTable where Employee_ID  = @EmployeeNo
  union all 
  select e.Employee_ID, e.SuperiorID
  from cte join EmployeeTable e on cte.SuperiorID = e.Employee_ID
  where e.Employee_ID <> '1000005' --president
)
select * from cte;

【讨论】:

  • 很抱歉,在我的询问会以上级的身份送达总统之前,我没有将其包含在内。
  • @Rigel1121 ,请参阅更新的 asnwer ,您需要在递归 cte 中添加条件
【解决方案3】:

试试这个:

DECLARE     @EmployeeTable  TABLE   (EmployeeID int, SuperiorID int);
INSERT INTO @EmployeeTable
    VALUES  
            (1000000, 1000001)
        ,   (1000001, 1000002)
        ,   (1000002, 1000003)
        ,   (1000003, 1000004)
        ,   (1000004, 1000005)
;

DECLARE @EmployeeNo int =   1000004
    ,   @President  int =   1000005
;

WITH    Employee    AS
    (
        SELECT 
                EmployeeID  =   @EmployeeNo
        UNION ALL
        SELECT  EmployeeID =  S.SuperiorID
        FROM    Employee        E
        JOIN    @EmployeeTable  S ON S.EmployeeID = E.EmployeeID
    )

SELECT * FROM Employee

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 2019-10-11
    • 2022-07-05
    • 2020-04-13
    • 2018-07-27
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多