创建一组 Months 作为 from 子句中的第一个表,并将您的查询加入到该表中。然后你会得到每个月的结果。我在财务报告方面有类似的问题,我需要所有月份和财政年度的结果。我使用了 DATENAME 函数来确保与您的查询结果一致。如果您想要按月份顺序(1 月 - 2 月 - 3 月)的数据,您可能不想按月份排序,因为这将按字母顺序排列,您需要包含一个排序字段。
SELECT M.[ Month ] AS [ Month ]
,SUM(ISNULL(x.total,0)) AS [Total] -- x.total will be null for months with no transactions.
FROM -- Set of Months (need one record for each month)
(SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
,(DATENAME(month,'2015-02-01'),2)
,(DATENAME(month,'2015-03-01'),3)
,(DATENAME(month,'2015-04-01'),4)
,(DATENAME(month,'2015-05-01'),5)
,(DATENAME(month,'2015-06-01'),6)
,(DATENAME(month,'2015-07-01'),7)
,(DATENAME(month,'2015-08-01'),8)
,(DATENAME(month,'2015-09-01'),9)
,(DATENAME(month,'2015-10-01'),10)
,(DATENAME(month,'2015-11-01'),11)
,(DATENAME(month,'2015-12-01'),12)) AS Mnth(" Month ",MnthSort)) AS M
LEFT OUTER JOIN -- Your from clause goes here.
(SELECT *
FROM (VALUES (DATENAME(month,'2015-01-01'),5)
,(DATENAME(month,'2015-02-01'),4)
,(DATENAME(month,'2015-02-01'),6)
,(DATENAME(month,'2015-03-01'),7)
,(DATENAME(month,'2015-04-01'),0)
,(DATENAME(month,'2015-05-01'),1)
,(DATENAME(month,'2015-05-01'),1)
) AS data(" Month ","total")) x ON x.[ Month ] = M.[ Month ]
GROUP BY M.[ Month ], M.MnthSort
ORDER BY M.MnthSort
我在 SQL Server 2008 - R1 上运行了这个
查询中 from 子句的第一部分以表格式定义月份集合,每个月返回一行(运行此命令以查看结果):
SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
,(DATENAME(month,'2015-02-01'),2)
,(DATENAME(month,'2015-03-01'),3)
,(DATENAME(month,'2015-04-01'),4)
,(DATENAME(month,'2015-05-01'),5)
,(DATENAME(month,'2015-06-01'),6)
,(DATENAME(month,'2015-07-01'),7)
,(DATENAME(month,'2015-08-01'),8)
,(DATENAME(month,'2015-09-01'),9)
,(DATENAME(month,'2015-10-01'),10)
,(DATENAME(month,'2015-11-01'),11)
,(DATENAME(month,'2015-12-01'),12)) AS Mnth(" Month ",MnthSort)
之后的 LEFT OUTER JOIN 是将查询结果链接到每个月,因此每个月都会得到一个总数。使用外部联接是因为没有每个月的总数。
从上面使用您的 sql 的查询将类似于:
SELECT M.[ Month ] AS [ Month ]
,SUM(ISNULL(x.total,0)) AS [Total] -- x.total will be null for months with no transactions.
FROM -- Set of Months (January - December), ensures one record for each month
(SELECT * FROM (VALUES(DATENAME(month,'2015-01-01'),1)
,(DATENAME(month,'2015-02-01'),2)
,(DATENAME(month,'2015-03-01'),3)
,(DATENAME(month,'2015-04-01'),4)
,(DATENAME(month,'2015-05-01'),5)
,(DATENAME(month,'2015-06-01'),6)
,(DATENAME(month,'2015-07-01'),7)
,(DATENAME(month,'2015-08-01'),8)
,(DATENAME(month,'2015-09-01'),9)
,(DATENAME(month,'2015-10-01'),10)
,(DATENAME(month,'2015-11-01'),11)
,(DATENAME(month,'2015-12-01'),12)) AS Mnth(" Month ",MnthSort)) AS M
LEFT OUTER JOIN -- Your Query included from here...
(SELECT datename(month,Resolved1Date) as ' Month ',
COUNT(CASE WHEN fileDescription NOT LIKE 'test%'
AND Issue1Description ='Escalated' THEN 0 ELSE 1
END) as 'total'
FROM complaint_1 WITH (nolock)
INNER JOIN Case WITH (nolock) ON Case.ref = complaint_1.ref
WHERE
Resolved1Date >=DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
Resolved1Date <= dateadd(mm,datediff(mm,0,getdate()),0)
group by datename(month,Resolved1Date), datepart(month, Resolved1Date)
) x on x.[ Month ] = M.[ Month ]
GROUP BY M.[ Month ], M.MnthSort
ORDER BY M.MnthSort