比CTE更好的方式

时间:2016-11-28 00:31:34

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

我写了一个简单的查询,但我觉得应该有更好的方法来做。我基本上正在寻找一种清理此代码的方法,以使其更加整洁,并在将来更加实用。我将在一些ssrs报告中使用它。

DECLARE @Month int = MONTH(GETDATE());
DECLARE @Year int = YEAR(GETDATE());
DECLARE @ThisMth DATE = DATEFROMPARTS(@Year,@Month,1);
DECLARE @BegYear DATE = DATEFROMPARTS(@Year, 7,1);

--EMPLOYEES
With EmpCount AS  
(
    SELECT  
        COUNT(DISTINCT(Employee)) As 'DistinctEmployees', PRCo 
    FROM 
        PREA 
    WHERE 
        Mth = @ThisMth 
    GROUP BY 
        PRCo),
TotalEmp AS  
(
    SELECT  
        COUNT(DISTINCT(Employee)) As 'TotalDistinctEmployees', PRCo 
    FROM 
        PREA 
    WHERE 
        Mth >= @BegYear 
    GROUP BY 
        PRCo),
Earnings AS
(
     SELECT 
         SUM(Amount) AS Earnings, PRCo 
     FROM 
         PREA 
     WHERE 
         Mth = @ThisMth AND EDLType = 'E' 
     GROUP BY 
         PRCo),
TotalEarnings AS 
(
     SELECT 
         SUM(Amount) as TotalEarnings, PRCo 
     FROM 
         PREA 
     WHERE 
         Mth >= @BegYear and EDLType = 'E' 
     GROUP BY 
         PRCo),
Deductions AS 
(
     SELECT 
         SUM(Amount) AS Deduction, PRCo 
     FROM 
         PREA 
     WHERE 
         Mth = @ThisMth AND EDLType = 'D' 
     GROUP BY PRCo),
TotalDeduction AS 
(
     SELECT 
         SUM(Amount) as TotalDed, PRCo 
     FROM 
         PREA 
     WHERE 
         Mth >= @BegYear AND EDLType = 'D' 
     GROUP BY 
         PRCo),
Liabilities AS 
(
    SELECT 
        SUM(Amount) AS Liab, PRCo 
    FROM 
        PREA 
    WHERE 
        Mth = @ThisMth AND EDLType = 'L' 
    GROUP BY
       PRCo),
TotalLiabilities AS 
(
    SELECT 
        SUM(Amount) as TotalLiab, PRCo 
    FROM 
        PREA 
    WHERE 
        Mth >= @BegYear AND EDLType = 'L' 
    GROUP BY 
        PRCo)
SELECT 
    a.PRCo, ec.DistinctEmployees, tem.TotalDistinctEmployees, 
    e.Earnings, te.TotalEarnings, d.Deduction, td.TotalDed, 
    l.Liab, tl.TotalLiab, te.TotalEarnings + tl.TotalLiab AS TotalCost
FROM 
    PREA a
INNER JOIN 
    EmpCount ec ON ec.PRCo = a.PRCo
INNER JOIN 
    Earnings e ON e.PRCo = a.PRCo
INNER JOIN 
    TotalEarnings te ON te.PRCo = a.PRCo
INNER JOIN 
    TotalEmp tem ON tem.PRCo = a.PRCo
INNER JOIN 
    Deductions d ON d.PRCo = a.PRCo
INNER JOIN 
    TotalDeduction td ON td.PRCo = a.PRCo
INNER JOIN 
    Liabilities l ON l.PRCo = a.PRCo
INNER JOIN 
    TotalLiabilities tl ON tl.PRCo =a.PRCo
WHERE 
    Mth = @ThisMth AND EDLType = 'E'
GROUP BY 
    a.PRCo, ec.DistinctEmployees,e.Earnings, te.TotalEarnings, 
    tem.TotalDistinctEmployees, d.Deduction, td.TotalDed, l.Liab, tl.TotalLiab

1 个答案:

答案 0 :(得分:2)

试试这个:

    SELECT PRCo
          ,COUNT(DISTINCT(CASE WHEN  Mth = @ThisMth  THEN Employee ELSE NULL END )) As 'DistinctEmployees'
          ,COUNT(DISTINCT(CASE WHEN  Mth >= @BegYear  THEN Employee ELSE NULL END )) As 'TotalDistinctEmployees'
          ,SUM(CASE WHEN Mth = @ThisMth AND EDLType = 'E' THEN  Amount ELSE 0 END ) AS Earnings
          ,SUM(CASE WHEN Mth >= @BegYear and EDLType = 'E' THEN  Amount ELSE 0 END) AS TotalEarnings
          ,SUM(CASE WHEN Mth = @ThisMth AND EDLType = 'D' THEN  Amount ELSE 0 END ) AS Deduction
          ,SUM(CASE WHEN Mth >= @BegYear and EDLType = 'D' THEN  Amount ELSE 0 END ) AS TotalDed
          ,SUM(CASE WHEN Mth = @ThisMth AND EDLType = 'L'  THEN  Amount ELSE 0 END ) AS Liab
          ,SUM(CASE WHEN Mth >= @BegYear and EDLType = 'L'  THEN  Amount ELSE 0 END ) AS TotalLiab
          ,SUM(CASE WHEN Mth >= @BegYear and EDLType like '[EL]' THEN  Amount ELSE 0 END) AS TotalCost
    FROM PREA AS p
    GROUP BY PRCo