初学者需要帮助按月,县和程序编写支出的SQL查询

时间:2015-10-21 21:44:59

标签: mysql sql-server select

我需要帮助编写一个SQL查询,它将按程序,县和月生成以下报告。

http://i.stack.imgur.com/wKImH.jpg

具有此信息的表包含以下列

enter image description here

我已经提出了这个问题,但它只会给我一月份的总数。有人可以告诉我如何在报告中获得剩下的几个月吗?

Select ServiceCounty, Program, Sum(TotalPaymentTotal) AS January 
FROM Table1
WHERE EntryDate BETWEEN '01/01/2014' AND '01/31/2014'
GROUP BY ServiceCounty, Program
ORDER BY ServiceCounty

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

试试这个:

示例:

create table table1 (servicecounty varchar(20), program varchar(20), totalpaymenttotal int, entrydate date);
insert into table1 values 
('Mo', 'Program1', 100, '2014-01-01'),('Mo', 'Program1', 100, '2014-03-01'),
('Foo', 'Program1', 100, '2014-02-01'),('Foo', 'Program1', 100, '2014-04-01'),
('Mo', 'Program2', 100, '2014-01-01'),('Mo', 'Program2', 100, '2014-03-01'),
('Foo', 'Program2', 100, '2014-02-01'),('Foo', 'Program2', 100, '2014-04-01');

查询:

select 
   servicecounty,
   program,
   sum(case when month(entrydate) = 1 then totalpaymenttotal else 0 end) as January,
   sum(case when month(entrydate) = 2 then totalpaymenttotal else 0 end) as February,
   sum(case when month(entrydate) = 3 then totalpaymenttotal else 0 end) as March,
   sum(case when month(entrydate) = 4 then totalpaymenttotal else 0 end) as April,
   sum(case when month(entrydate) = 5 then totalpaymenttotal else 0 end) as May,
   sum(case when month(entrydate) = 6 then totalpaymenttotal else 0 end) as June,
   sum(case when month(entrydate) = 7 then totalpaymenttotal else 0 end) as July,
   sum(case when month(entrydate) = 8 then totalpaymenttotal else 0 end) as August,
   sum(case when month(entrydate) = 9 then totalpaymenttotal else 0 end) as September,
   sum(case when month(entrydate) = 10 then totalpaymenttotal else 0 end) as October,
   sum(case when month(entrydate) = 11 then totalpaymenttotal else 0 end) as November,
   sum(case when month(entrydate) = 12 then totalpaymenttotal else 0 end) as December
from table1
where year(entrydate) = 2014 -- gives you data for each month of this year
group by servicecounty, program

结果:

| servicecounty |  program | January | February | March | April | May | June | July | August | September | October | November | December |
|---------------|----------|---------|----------|-------|-------|-----|------|------|--------|-----------|---------|----------|----------|
|           Foo | Program1 |       0 |      100 |     0 |   100 |   0 |    0 |    0 |      0 |         0 |       0 |        0 |        0 |
|            Mo | Program1 |     100 |        0 |   100 |     0 |   0 |    0 |    0 |      0 |         0 |       0 |        0 |        0 |
|           Foo | Program2 |       0 |      100 |     0 |   100 |   0 |    0 |    0 |      0 |         0 |       0 |        0 |        0 |
|            Mo | Program2 |     100 |        0 |   100 |     0 |   0 |    0 |    0 |      0 |         0 |       0 |        0 |        0 |

SQLFiddle示例:http://sqlfiddle.com/#!6/d0c80/1

答案 1 :(得分:1)

您需要通过汇总将EntryDate的年份和月份添加到您的组中。 在sql server中有内置函数来提取年份和版本。日期时间字段的月份如下:

Select ServiceCounty, Program , YEAR(EntryDate) as Year,
       MONTH(EntryDate) as Month, Sum(TotalPaymentTotal) AS SumTotal  
FROM Table1  
GROUP BY YEAR(EntryDate), MONTH(EntryDate), ServiceCounty, Program  
ORDER BY ServiceCounty

如果您需要按日期时间的任意部分进行分组,则可以使用DATEPART功能。

这将为您提供每个月/每年的长格式化数据。如果您真的需要将这些月份列入列,请使用Sql Server的PIVOT功能。