SQL显示计数为0时的月份
我想知道是否有人能帮我 下面的SQL查询将其缩短为大型联合查询SQL显示计数为0时的月份,sql,date,count,sql-server-2008-r2,datepart,Sql,Date,Count,Sql Server 2008 R2,Datepart,我想知道是否有人能帮我 下面的SQL查询将其缩短为大型联合查询 SELECT [ Month ], sum(total) from (select datename(month,Resolved1Date) as ' Month ', COUNT(case when fileDescription not like 'test%' and Issue1Description ='Escalated' then 0 else 1 end) as 'total' F
SELECT [ Month ],
sum(total)
from
(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
group by [ Month ]
order by [ Month ] desc
有人能给我指引正确的方向吗?您可以创建临时表并从中左连接 大概是这样的:
DECLARE @Helper TABLE
(
TheDate datetime
)
DECLARE @StartDate datetime
SELECT @StartDate = '01.01.2015'
WHILE @StartDate < DATEADD(day,7,GETDATE())
BEGIN
INSERT INTO @Helper (Thedate) VALUES (@StartDate)
SELECT @StartDate = DATEADD(MONTH, 1, @StartDate)
END
我希望它能有所帮助。在from子句中创建一组月份作为第一个表,并将您的查询加入到这个表中。然后你每个月都会得到一个结果。 我在财务报告方面也有类似的问题,我需要所有月份和财年的结果。 我使用了DATENAME函数来确保结果与您的查询一致。 如果您希望数据按月份顺序为一月-二月-三月,您可能不希望按月份排序,因为这是按字母顺序排列的,您需要包含一个排序字段
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
我在SQLServer2008-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)
后面的左外部联接用于将查询结果链接到每个月,因此每个月都会得到一个总数。使用外部联接是因为没有每个月的合计
使用上面的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
您不能只为ResolvedDate=0添加另一个WHERE子句吗?您应该格式化查询。您还应该修正逻辑。例如,countcase中的case语句没有任何作用。你也应该用你正在使用的数据库来标记这个问题。删除case语句无助于我的目标,我认为问题在于计数,因为我在计算一个日期字段,然后说明sate字段在两个值之间的位置,如果不匹配,我需要它将一个月显示为0。谢谢你的帖子@nxb,你能解释一下,在from子句中创建一组月份作为第一个表是什么意思吗?我对SQL还很陌生,这让我开始发疯了!编辑帖子回答问题,希望更清楚。啊,明白了,我已经注释掉了“左外连接”并输入了我的查询,并且收到了错误的语法消息。我不明白它是用来把我的查询结果和月份联系起来的,现在有意义了。感谢您的帮助@nxb:-非常感谢
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