2015-08-26 83 views
5

我目前很難從我的表中獲取正確的值。這裏是我的表從一個表中分組SQL列

注:列Status有3個可能的值(潔淨,不潔淨,封閉)

+-----------+-------------+--------+------------+ 
    |ApplicantID|ApplicantName| Status | HireDate | 
    +-----------+-------------+--------+------------+ 
    |  1  | John Smith |Cleaned |08/26/2015 | 
    |  2  | Alex Murphy |Closed |09/12/2015 | 
    |  3  | Oliver David|Cleaned |01/11/2015 | 
    |  4  | Max Payne |Unclean |03/18/2015 | 
    +-----------+-------------+--------+------------+ 

我期待的輸出,它也應年份排序。 例如,我將所有這些記錄稱爲2015年,我使用變量@Year

注:列TotalCleaned之和Unclean

+---------+-----------+-----------+----------+---------+ 
    | Month | Cleaned | Unclean | Closed | Total | 
    +---------+-----------+-----------+----------+---------+ 
    | January|  1  |  0  | 0  | 1 | 
    | February|  0  |  0  | 0  | 0 | 
    | March |  0  |  1  | 0  | 1 | 
    | April |  0  |  0  | 0  | 0 | 
    | May |  0  |  0  | 0  | 0 | 
    | June |  0  |  0  | 0  | 0 | 
    | July |  0  |  0  | 0  | 0 | 
    | August |  1  |  0  | 0  | 1 | 
    |September|  0  |  0  | 1  | 0 | 
    | October|  0  |  0  | 0  | 0 | 
    | November|  0  |  0  | 0  | 0 | 
    | December|  0  |  0  | 0  | 0 | 
    +---------+-----------+-----------+----------+---------+ 

我似乎無法得到正確的代碼,對SQL這是我當前的代碼。

SELECT Month(HireDate) AS Month, COUNT(*) 
FROM Hires 
GROUP BY Month(HireDate) 

我知道我的編碼是錯誤的,因爲它是不完整的。

回答

3

生成一個數字列表,從112先保存所有month s。然後在Hires上執行LEFT JOIN以確保所有遺漏的月份都被計入。然後使用條件聚集了總計:

SQL Fiddle

;WITH CteMonths AS(
    SELECT * FROM(VALUES 
     (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12) 
    )t(N) 
) 
SELECT 
    Month = DATENAME(MONTH, DATEADD(MONTH, N-1,0)), 
    Cleaned = SUM(CASE WHEN h.Status = 'Cleaned' THEN 1 ELSE 0 END), 
    Closed = SUM(CASE WHEN h.Status = 'Closed' THEN 1 ELSE 0 END), 
    Unclean = SUM(CASE WHEN h.Status = 'Unclean' THEN 1 ELSE 0 END), 
    Total = SUM(CASE WHEN h.Status IN('Cleaned', 'Unclean') THEN 1 ELSE 0 END) 
FROM CteMonths m 
LEFT JOIN Hires h 
    ON m.N = MONTH(h.HireDate) 
    --AND YEAR(h.HireDate) = @year --uncomment this line to filter for year. 
GROUP BY m.N 
ORDER BY m.N 

如果要包括年份:

SQL Fiddle

;WITH CteMonths AS(
    SELECT * FROM(VALUES 
     (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12) 
    )t(N) 
), 
CteYears(yr) AS(
    SELECT DISTINCT YEAR(HireDate) FROM Hires 
), 
CteAllDates(dt) AS(
    SELECT 
     DATEADD(MONTH, m.N - 1, DATEADD(YEAR, y.yr - 1900, 0)) 
    FROM CteMonths m 
    CROSS JOIN CteYears y 
) 
SELECT 
    Year = YEAR(d.dt), 
    Month = DATENAME(MONTH, d.dt), 
    Cleaned = SUM(CASE WHEN h.Status = 'Cleaned' THEN 1 ELSE 0 END), 
    Closed = SUM(CASE WHEN h.Status = 'Closed' THEN 1 ELSE 0 END), 
    Unclean = SUM(CASE WHEN h.Status = 'Unclean' THEN 1 ELSE 0 END), 
    Total = SUM(CASE WHEN h.Status IN('Cleaned', 'Unclean') THEN 1 ELSE 0 END) 
FROM CteAllDates d 
LEFT JOIN Hires h 
    ON MONTH(d.dt) = MONTH(h.HireDate) 
    AND YEAR(d.dt) = YEAR(h.HireDate) 
GROUP BY YEAR(d.dt), MONTH(d.dt), DATENAME(MONTH, d.dt) 
ORDER BY YEAR(d.dt), MONTH(d.dt) 

如果你要篩選一年,說@year = 2015,你可以替換T.他與以前的熱膨脹係數:

;WITH CteMonths AS(
    SELECT * FROM(VALUES 
     (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12) 
    )t(N) 
), 
CteAllDates(dt) AS(
    SELECT 
     DATEADD(MONTH, m.N - 1, DATEADD(YEAR, @year - 1900, 0)) 
    FROM CteMonths m 
)... 
+0

是的,因爲'SUM'聚合。 –

+2

等一下......你是@wewesthemenace改名!我不知道所以讓你改變用戶名。 –

+0

@TimBiegeleisen是的,這是我。 –

0

我建議從112(月數)和JOIN您與TEMP表的表創建TEMP表值。要獲得列名稱的值,可以使用PIVOTCASE。你能做到在以下幾點:

INSERT INTO #Months VALUES 
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12) 

SELECT DATENAME(MONTH, DATEADD(MONTH, m.Id-1, 0)) AS [Month] 
     , SUM(CASE WHEN [Status] = 'Cleaned' THEN 1 ELSE 0 END) AS [Cleaned] 
     , SUM(CASE WHEN [Status] = 'Closed' THEN 1 ELSE 0 END) AS [Closed] 
     , SUM(CASE WHEN [Status] = 'Unclean' THEN 1 ELSE 0 END) AS [Unclean] 
     , SUM(CASE WHEN [Status] IN ('Unclean', 'Cleaned') THEN 1 ELSE 0 END) AS [Total] 
FROM #Test t 
RIGHT JOIN #Months m ON m.Id = MONTH(t.HireDate) 
GROUP BY m.Id 

輸出

+---------+-----------+-----------+----------+---------+ 
    | Month | Cleaned | Unclean | Closed | Total | 
    +---------+-----------+-----------+----------+---------+ 
    | January |  1  |  0  | 0  | 1 | 
    | February|  0  |  0  | 0  | 0 | 
    | March |  0  |  1  | 0  | 1 | 
    | April |  0  |  0  | 0  | 0 | 
    | May |  0  |  0  | 0  | 0 | 
    | June |  0  |  0  | 0  | 0 | 
    | July |  0  |  0  | 0  | 0 | 
    | August |  1  |  0  | 0  | 1 | 
    |September|  0  |  0  | 1  | 0 | 
    | October |  0  |  0  | 0  | 0 | 
    | November|  0  |  0  | 0  | 0 | 
    | December|  0  |  0  | 0  | 0 | 
    +---------+-----------+-----------+----------+---------+ 

DEMO

您可以測試它:SQL FIDDLE