Question

I have data in database as follows:

+---------------------+-------------+
|        CloseTime    |       Count |
+---------------------+-------------+
|     10.2.2014 13:19 |   1         |
|     5.12.2014 13:19 |   1         |
|     4.2.2014 13:19  |   1         |
|     2.1.2014 13:19  |   1         |
|     4.12.2014 13:19 |   1         |
+---------------------+-------------+

Now, I have the query like this

SELECT DATENAME(MONTH, CLOSETIME) AS CLOSEMONTH, COUNT(*) as CNT
FROM TABLE
WHERE CLOSETIME >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE())-7, '19000101')
AND CLOSETIME <  DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
GROUP BY DATENAME(MONTH, CLOSETIME);

which result in

+---------------+-------+
|     CloseTime |  CNT  |
+---------------+-------+
|     February  | 2     |
|     January   | 1     |
|     December  | 2     |
+---------------+-------+

The query works in the way that it takes data for last 7 months. I would need the result to be

+---------------+-------+
|     CloseTime | Count |
+---------------+-------+
|     February  | 2     |
|     January   | 1     |
|     December  | 2     |
|     November  | 0     |
|     October   | 0     |
|     September | 0     |
|     August    | 0     |
+---------------+-------+

so if there are no records with Close Time for later months, it should still return the month with zero count. How to achieve this? I prefer SQL solution only, meaning no procedures or scripts.

I tried to

SELECT DATENAME(MONTH,DATEADD(MONTH,-1,GETDATE())) as HELP_MONTH
UNION ALL
...

and then join it, but it has not success.

Was it helpful?

Solution 3

OK, this worked for me:

WITH HM (HELP_MONTH) AS (
SELECT DATENAME(MONTH,DATEADD(MONTH,-1,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-2,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-3,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-4,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-5,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-6,GETDATE())) AS HELP_MONTH
UNION ALL
SELECT DATENAME(MONTH,DATEADD(MONTH,-7,GETDATE())) AS HELP_MONTH)
SELECT HELP_MONTH
,COUNT(CNT) AS CNT
FROM HM
LEFT JOIN YOUR_TABLE ON HM.HELP_MONTH = DATENAME(MONTH, CLOSETIME)
AND CLOSETIME >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE())-7, '19000101')
AND CLOSETIME <  DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
GROUP BY DATENAME(MONTH, CLOSETIME), HELP_MONTH

OTHER TIPS

Use the same query by adding HAVING clause will gonna add records having 0 count also.

SELECT DATENAME(MONTH, CLOSETIME) AS CLOSEMONTH, COUNT(*) as CNT
FROM TABLE
WHERE CLOSETIME >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE())-7, '19000101')
AND CLOSETIME <  DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
GROUP BY DATENAME(MONTH, CLOSETIME)
HAVING COUNT(*) >= 0

Thanks.

Following query can be LEFT JOIN ed with the your resultset:

SELECT * FROM
(
   SELECT 'January' AS month_name, 1 AS month_num
   UNION
   SELECT 'February',2
   UNION 
   SELECT 'Mar',3
   UNION 
   SELECT 'Apr',4
   UNION 
   SELECT 'May',5
   UNION 
   SELECT 'June',6
   UNION 
   SELECT 'July',7
   UNION
   SELECT 'August', 8
   UNION
   SELECT 'September', 9
   UNION
   SELECT 'October', 10
   UNION
   SELECT 'November', 11
   UNION
   SELECT 'December', 12
)
months
WHERE month_num <= MONTH(GETDATE())
OR month_num  > (MONTH(GETDATE()) - 7) % 12 + 12 

The above query gives last 7 months as resultset:

Here is the code at SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top