按年,月和条件选择和分组

时间:2014-04-10 12:30:50

标签: sql sql-server select group-by

我有一个这样的表(这只是一个例子):

+-------------+---------------------+---------------------+
| status      | open_date           | close_date          |
+-------------+---------------------+---------------------+
|      closed | 01-11-2014 19:32:44 | 01-11-2014 20:32:44 |
|        open | 01-12-2014 22:33:49 | 02-12-2014 22:33:49 |
|        open | 01-23-2014 22:08:24 | 03-23-2014 22:08:24 |
|      closed | 02-01-2014 22:33:57 | 03-01-2014 22:33:57 |
|        open | 02-01-2013 22:37:34 | 02-01-2013 23:37:34 |
|      closed | 04-20-2013 15:23:00 | 05-20-2013 15:23:00 |
|        open | 04-20-2013 12:21:49 | 05-20-2013 12:21:49 |
|      closed | 04-25-2013 11:22:00 | 06-25-2013 11:22:00 |
|      closed | 05-20-2013 14:23:49 | 10-20-2013 14:23:49 |
|      closed | 04-20-2013 16:33:49 | 04-25-2013 16:33:49 |
+-------------+---------------------+---------------------*

并希望按年份和月份列出所有已打开和已关闭的案例,如下所示:

+-------------+---------------+--------------+
| Year | Month | Opened Cases | Closed Cases |
+-------------+---------------+--------------+
| 2014 |     4 |           10 |            5 |
| 2014 |     3 |            9 |            7 |           
| 2014 |     2 |           15 |           10 |
| 2014 |     1 |           12 |            1 |
| 2013 |    12 |           30 |            9 |
| 2013 |    11 |            5 |           50 |
+--------------+--------------+--------------+

我有这样的选择:

SELECT
  YEAR(open_date) AS TheYear,
  MONTH(open_date) AS TheMonth,
  sum(CASE WHEN open_date = ??? THEN 1 ELSE 0 END) TheOpened
  sum(CASE WHEN close_date = ??? THEN 1 ELSE 0 END) TheClosed
FROM
  TABLE
WHERE
   CASEGROUP= 'SUPPORT'
GROUP BY
  MONTH(open_date),
  YEAR(open_date)
ORDER BY
  TheYear DESC,
  TheMonth ASC

3 个答案:

答案 0 :(得分:0)

试试这个

SELECT
  YEAR(open_date) AS TheYear,
  MONTH(open_date) AS TheMonth,
  sum(CASE WHEN open_status= 'Closed' THEN 0 ELSE 1 END) TheOpened,
  sum(CASE WHEN open_status= 'Closed' THEN 1 ELSE 0 END) TheClosed
FROM
  TABLE
WHERE
   CASEGROUP= 'SUPPORT'
GROUP BY
  MONTH(open_date),
  YEAR(open_date)
ORDER BY
  TheYear DESC,
  TheMonth ASC

答案 1 :(得分:0)

   select [Year], [Month], Sum([Opened Cases]), sum([Closed Cases]) from (
    select year(open_date) as [Year], MONTH(open_date) as [Month], count(*) as [Opened Cases] ,0 as [Closed Cases]
    from [table] where status='open'
    group by year(open_date), MONTH(open_date)
    union
    select year(close_date), MONTH(close_date), 0,count(*) 
    from [table] where status='closed'
    group by year(close_date), MONTH(close_date)) as o
    group by [Year], [Month]

答案 2 :(得分:0)

找到它。

SELECT
  YEAR(open_time) AS TheYear,
  MONTH(open_time) AS TheMonth,
  sum(CASE WHEN DATEPART(YYYY, open_time)= YEAR(open_time) AND DATEPART(MM, open_time)= MONTH(open_time) THEN 1 ELSE 0 END) TheOpened,
  sum(CASE WHEN status = 'closed'  THEN 1 ELSE 0 END) TheClosed
FROM
  TABLE
WHERE
  CASEGROUP= 'SUPPORT'
GROUP BY
  MONTH(open_date),
  YEAR(open_date)
ORDER BY
  TheYear DESC,
  TheMonth ASC