2015-10-23 19 views
1

我需要生成支出清单&按月申请的数量,县&程序。到目前为止,我已经能够获得支出清单,但我无法获得每月申请数量的列表。这是我迄今为止的查询,但应用程序的数量不正确。SQL查询:按月,县,和程序的申请数量

select 
servicecounty AS County, 
program, 
sum(case when month(entrydate) = 1 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as January, 
sum(case when month(entrydate) = 2 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as February, 
sum(case when month(entrydate) = 3 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as March, 
sum(case when month(entrydate) = 4 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as April, 
sum(case when month(entrydate) = 5 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as May, 
sum(case when month(entrydate) = 6 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as June, 
sum(case when month(entrydate) = 7 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as July, 
sum(case when month(entrydate) = 8 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as August, 
sum(case when month(entrydate) = 9 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as September, 
sum(case when month(entrydate) = 10 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as October, 
sum(case when month(entrydate) = 11 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as November, 
sum(case when month(entrydate) = 12 and year(entrydate) = 2014 then totalpaymenttotal else 0 end) as December, 
sum(case when month(entrydate) = 1 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [January 15], 
sum(case when month(entrydate) = 2 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [February 15], 
sum(case when month(entrydate) = 3 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [March 15], 
sum(case when month(entrydate) = 4 and year(entrydate) = 2015 then totalpaymenttotal else 0 end) as [April 15] 
from Sheet$ 
group by servicecounty, program 

UNION ALL 
select 
servicecounty AS County, 
program, 
COUNT(case when month(entrydate) = 1 and year(entrydate) = 2014 then ApplicationID else 0 end) as January, 
count(case when month(entrydate) = 2 and year(entrydate) = 2014 then ApplicationID else 0 end) as February, 
count(case when month(entrydate) = 3 and year(entrydate) = 2014 then ApplicationID else 0 end) as March, 
count(case when month(entrydate) = 4 and year(entrydate) = 2014 then ApplicationID else 0 end) as April, 
count(case when month(entrydate) = 5 and year(entrydate) = 2014 then ApplicationID else 0 end) as May, 
count(case when month(entrydate) = 6 and year(entrydate) = 2014 then ApplicationID else 0 end) as June, 
count(case when month(entrydate) = 7 and year(entrydate) = 2014 then ApplicationID else 0 end) as July, 
count(case when month(entrydate) = 8 and year(entrydate) = 2014 then ApplicationID else 0 end) as August, 
count(case when month(entrydate) = 9 and year(entrydate) = 2014 then ApplicationID else 0 end) as September, 
count(case when month(entrydate) = 10 and year(entrydate) = 2014 then ApplicationID else 0 end) as October, 
count(case when month(entrydate) = 11 and year(entrydate) = 2014 then ApplicationID else 0 end) as November, 
count(case when month(entrydate) = 12 and year(entrydate) = 2014 then ApplicationID else 0 end) as December, 
Count(case when month(entrydate) = 1 and year(entrydate) = 2015 then ApplicationID else 0 end) as [January 15], 
Count(case when month(entrydate) = 2 and year(entrydate) = 2015 then ApplicationID else 0 end) as [February 15], 
Count(case when month(entrydate) = 3 and year(entrydate) = 2015 then ApplicationID else 0 end) as [March 15], 
Count(case when month(entrydate) = 4 and year(entrydate) = 2015 then   ApplicationID else 0 end) as [April 15] 
from Sheet$ 
group by servicecounty, program 
ORDER BY program 

下面是报告中应该是什么样子:每月

支出,程序&县: http://i.stack.imgur.com/yJ26A.jpg

按月应用数量,计划&县: http://i.stack.imgur.com/7Aqkk.png

该表包含以下字段:ServiceCounty,TotalPaymentTotal,Program,ApplicationID,EntryDate

在此先感谢您的帮助。

+0

您可能需要在总和中加1,以便计算行数 –

+0

您应该用您真正使用的数据库(MySQL或SQL Server)来标记您的问题。 –

回答

0

您在第二个子查询中的计数都返回相同的值。请记住,count()计算非NULL值的数量。并且,0不为空。

三种解决方案:

  • 更改count()sum()thenthen 1
  • 删除else
  • else 0更改为else NULL

这些都是我个人的偏好。

+0

还有一个问题,我想格式化输出,以便结果集中不出现小数。我试过了,总和前面的演员功能和总和之后,但没有运气。总和(当月(entrydate)= 1且year(entrydate)= 2014 then 1 else 0 end)的情况下,作为January, – Marodr

+0

@Marodr。 。 。这听起来像是一个不同的问题。 –