CTE递归查询

时间:2014-06-23 13:16:06

标签: sql sql-server recursion common-table-expression

我有一张包含员工记录的表格,其中显示了员工与他们向谁报告的关系:

From_ID   position          TO_ID   position
----------------------------------------
1         Lowest_employee   3       employee
3         employee          4       employee
4         employee          5       BOSS
2         Lowest_employee   6       employee
6         employee          3       employee
10        Lowest_employee   50      BOSS2

我想用员工/老板ID显示看起来像这样的结果:

EmployeeID   BossID
--------------------
1            5
2            5
10           50

enter image description here

这意味着员工1和2向ID 5报告,员工10向另一个ID为50的老板报告。

我知道我需要使用CTE和递归查询,但无法理解它是如何完成的,我对CTE递归查询更新。

我读过这篇文章,但它对我没有任何意义MS link

实现此目的所需的查询帮助将非常有用。

3 个答案:

答案 0 :(得分:2)

WITH    q (employee, boss) AS
        (
        SELECT  fromId, toId
        FROM    mytable
        WHERE   fromId NOT IN
                (
                SELECT  toId
                FROM    mytable
                )
        UNION ALL
        SELECT  employee, toId
        FROM    q
        JOIN    mytable t
        ON      t.fromId = boss
        )
SELECT  *
FROM    q
WHERE   boss NOT IN
        (
        SELECT  fromId
        FROM    mytable
        )

答案 1 :(得分:2)

这包括设置测试数据,但我认为这是你想要的:

测试数据:

DECLARE @Table TABLE
(
    From_ID int, 
    TO_ID int
)
INSERT INTO @Table VALUES(1,3)
INSERT INTO @Table VALUES(3,4)
INSERT INTO @Table VALUES(4,5)
INSERT INTO @Table VALUES(2,6)
INSERT INTO @Table VALUES(6,3)
INSERT INTO @Table VALUES(10,50)

查询以获得答案:

;WITH Hierarchy (Employee, Superior, QueryLevel)
AS
(
    --root is all employees that have no subordinates
    SELECT  E.From_ID, E.TO_ID, 1
    FROM    @Table E
    LEFT
    JOIN    @Table S
            ON  S.TO_ID = E.From_ID
    WHERE   S.TO_ID IS NULL
    --recurse up tree to final superior
    UNION ALL
    SELECT  H.Employee, S.TO_ID, H.QueryLevel + 1
    FROM    Hierarchy H
    JOIN    @Table S
            ON  S.From_ID = H.Superior
)
SELECT Employee, Superior 
FROM
(
    SELECT  *, ROW_NUMBER() OVER(PARTITION BY Employee ORDER BY QueryLevel DESC) AS RowNumber 
    FROM Hierarchy
) H
WHERE RowNumber = 1

基本上,这适用于:

1)让没有报告人(根)的所有员工

2)通过老板递响,记录'等级'

3)使用over / partition只选择'final'boss

答案 2 :(得分:2)

你可以试试这样的东西吗?

DECLARE @Employees TABLE (
    EmployeeId INT,
    PositionName VARCHAR(50),
    ReportsToId INT);
INSERT INTO @Employees VALUES (1, 'Driver', 3);
INSERT INTO @Employees VALUES (3, 'Head of Driving Pool', 4);
INSERT INTO @Employees VALUES (4, 'Corporate Flunky', 5);
INSERT INTO @Employees VALUES (2, 'Window Cleaner', 6);
INSERT INTO @Employees VALUES (6, 'Head of Office Services', 3);
INSERT INTO @Employees VALUES (10, 'Minion', 50);
INSERT INTO @Employees VALUES (5, 'BOSS', NULL);
INSERT INTO @Employees VALUES (50, 'BOSS2', NULL);
WITH Employees AS (
    SELECT
        EmployeeId,
        1 AS [Level],
        EmployeeID AS [Path],
        ISNULL(ReportsToId, EmployeeId) AS ReportsToId
    FROM 
        @Employees
    WHERE 
        ReportsToId IS NULL
    UNION ALL
    SELECT
        e.EmployeeID,
        x.[Level] + 1 AS [Level],
        x.[Path] + e.EmployeeID AS [Path],
        x.ReportsToId
    FROM 
        @Employees e
        INNER JOIN Employees x ON x.EmployeeID = e.ReportsToId)
SELECT
    ec.EmployeeId,
    e.PositionName,
    ec.[Level],
    CASE WHEN ec.ReportsToId = ec.EmployeeId THEN NULL ELSE ec.ReportsToId END AS ReportsToId --Can't really report to yourself
FROM 
    Employees ec
    INNER JOIN @Employees e ON e.EmployeeId = ec.EmployeeId
ORDER BY 
    ec.[Path];