#SQL UNION两个WITH SELECT

时间:2018-06-19 17:35:51

标签: sql sql-server

我试图使两个SELECT语句的UNION与WITH,

选择是对经理的递归搜索和对经理的所有下属的递归搜索,如果EmployeeNumber具有像下属这样的Managers,则仅映射此对象,而不是此经理的下属,但是如果其下属是而不是经理,他们和所有下属的递归搜索都被映射。

JobCode表具有经理的代码

查询例如:

DECLARE @EmployeeNumber VARCHAR(10) = '1035423';

 WITH EmpCTE (Id, 
              Name, 
              Email, 
              EmployeeNumber, 
              Login, 
              Status, 
              SupervisorNumber, 
              Shift, 
              CostCenter, 
              JobCode)
   AS (SELECT Id,
              Name,
              Email,
              EmployeeNumber,
              Login,
              Status,
              SupervisorNumber,
              Shift,
              CostCenter,
              JobCode
         FROM dbo.Employees
        WHERE EmployeeNumber = @EmployeeNumber
    UNION ALL
       SELECT e.Id,
              e.Name,
              e.Email,
              e.EmployeeNumber,
              e.Login,
              e.Status,
              e.SupervisorNumber,
              e.Shift,
              e.CostCenter,
              e.JobCode
         FROM dbo.Employees AS e
         JOIN EmpCTE AS m
           ON (e.SupervisorNumber = m.EmployeeNumber 
          AND e.JobCode IN  (SELECT Id FROM dbo.JobCodes)))
       SELECT *
         FROM EmpCTE
        UNION

WITH EmpCTE2 (Id, 
              Name, 
              Email, 
              EmployeeNumber, 
              Login, 
              Status, 
              SupervisorNumber, 
              Shift, 
              CostCenter, 
              JobCode)
   AS (SELECT Id,
              Name,
              Email,
              EmployeeNumber,
              Login,
              Status,
              SupervisorNumber,
              Shift,
              CostCenter,
              JobCode
         FROM dbo.Employees
        WHERE EmployeeNumber = @EmployeeNumber
    UNION ALL
       SELECT e.Id,
              e.Name,
              e.Email,
              e.EmployeeNumber,
              e.Login,
              e.Status,
              e.SupervisorNumber,
              e.Shift,
              e.CostCenter,
              e.JobCode
         FROM dbo.Employees AS e
         JOIN EmpCTE2 AS m
           ON (e.SupervisorNumber = m.EmployeeNumber 
          AND e.JobCode NOT IN (SELECT Id FROM dbo.JobCodes)))
       SELECT *
         FROM EmpCTE2;

有人知道如何执行此操作,或者不使用指针就可以执行此选择

2 个答案:

答案 0 :(得分:2)

您必须将所有CTE放在顶部,然后才能在完整的SELECT中使用它们:

WITH cte1 AS (...)
,   cte2 AS (...)
SELECT...FROM cte1
UNION 
SELECT...FROM cte2

答案 1 :(得分:0)

或者您可以像下面这样在一个递归cte中完成

DECLARE @EmployeeNumber VARCHAR(10) = '1035423';

WITH EmpCTE (Id, Name, Email, EmployeeNumber, Login, Status, SupervisorNumber, Shift, CostCenter, JobCode,stop)
AS (
SELECT Id,
       Name,
       Email,
       EmployeeNumber,
       Login,
       Status,
       SupervisorNumber,
       Shift,
       CostCenter,
       JobCode,
       stop=null
    FROM dbo.Employees
    WHERE EmployeeNumber = @EmployeeNumber
    UNION ALL
    SELECT e.Id,
           e.Name,
           e.Email,
           e.EmployeeNumber,
           e.Login,
           e.Status,
           e.SupervisorNumber,
           e.Shift,
           e.CostCenter,
           e.JobCode,
           stop= case when e.JobCode IN (SELECT Id FROM dbo.JobCodes) then 1 else NULL end
    FROM dbo.Employees AS e
        JOIN EmpCTE AS m
            ON (e.SupervisorNumber = m.EmployeeNumber)
            AND (m.Stop is null )
)
SELECT Id, Name, Email, EmployeeNumber, Login, Status, SupervisorNumber, Shift, CostCenter, JobCode
FROM EmpCTE