如何显示在CTE和连接查询中写入的页脚部分上的列总和

时间:2017-05-05 10:08:18

标签: sql sql-server tsql sql-server-ce

我以相邻的方式得到总数,所以我想如何显示在CTE和连接查询中写入的页脚部分的列总和

        1  2  3
        1  1  1
    Tot 2  0  0
        0  3  0
        0  0  4

查询是:

;WITH t1
AS (
    SELECT DeptID
        ,COUNT(EmpID) AS NewJoinees
    FROM EmployeeDetails AS EmployeeDetails_7
    WHERE (DATEPART(yyyy, DOJ) = 2017)
        AND (DATEPART(mm, DOJ) = 02)
        AND datepart(mm, DOJ) >= 02
        AND STATUS = 0
    GROUP BY DeptID
    WITH ROLLUP
    )
    ,t3
AS (
    SELECT DeptID
        ,COUNT(EmpID) AS Resigned
    FROM EmployeeDetails AS EmployeeDetails_7
    WHERE (DATEPART(yyyy, deactivate) = 2017)
        AND (DATEPART(mm, deactivate) = 02)
        AND datepart(mm, deactivate) >= 02
        AND STATUS = 1
    GROUP BY DeptID
    WITH ROLLUP
    )
    ,t4
AS (
    SELECT DISTINCT DeptID
        ,Tobehired AS ToBeHired
        ,Openposition AS OpenPositions
        ,STATUS AS Status1
    FROM RecruitmentDetails1
    WHERE Tobehired = 45
    )
    ,t5
AS (
    SELECT DeptID
        ,Deptname
    FROM DepartmentDetails
    WHERE DeptID <> 'BPO'
        AND DeptID <> 'Finance'
        AND DeptID <> 'Accounts'
        AND DeptID <> 'Mgmt'
    )
SELECT t5.Deptname AS Department
    ,CASE 
        WHEN TotalHeadCount IS NULL
            THEN '0'
        ELSE TotalHeadCount
        END AS TotalHeadCount
    ,CASE 
        WHEN NewJoinees IS NULL
            THEN '0'
        ELSE NewJoinees
        END AS NewJoinees
    ,CASE 
        WHEN Resigned IS NULL
            THEN '0'
        ELSE Resigned
        END AS Resigned
    ,t4.Tobehired AS ToBeHired
    ,t4.OpenPositions AS OpenPositions
    ,t4.Status1 AS Status1
FROM t1
FULL OUTER JOIN t2 ON t1.DeptID = t2.DeptID
FULL OUTER JOIN t3 ON t1.DeptID = t3.DeptID
FULL OUTER JOIN t4 ON t1.DeptID = t4.DeptID
FULL OUTER JOIN t5 ON t1.DeptID = t5.DeptID

0 个答案:

没有答案