从一个表中分组SQL列

时间:2015-08-26 05:58:03

标签: sql-server

我目前很难从我的表中获取正确的值。这是我的表

注意:列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

注意:列TotalCleanedUnclean

的和
   +---------+-----------+-----------+----------+---------+
   |  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)

我知道我的编码错了,因为它不完整。

2 个答案:

答案 0 :(得分:3)

首先生成从12month的数字列表,以保留所有LEFT JOIN个。然后在Hires上执行;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:

SQL Fiddle

@year = 2015

如果你想过滤年份,比如;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 )... ,你可以用以下代码替换以前的ctes:

core.autocrlf

答案 1 :(得分:0)

我建议使用TEMP1(月数)和12表格JOIN创建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    |
   +---------+-----------+-----------+----------+---------+

<强>样本

您可以在以下位置对其进行测试: SQL FIDDLE