Question

I have a table like this (this is really an example only):

+-------------+---------------------+---------------------+
| 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 |
+-------------+---------------------+---------------------*

And want to list all opened and closed cases by Year and Month, like this:

+-------------+---------------+--------------+
| 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 |
+--------------+--------------+--------------+

I have a select like this:

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
Was it helpful?

Solution 3

Found it.

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

OTHER TIPS

Try this

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
   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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top