日期 分公司 数量
2007-12-26 1 2
2007-12-30 2 5
2007-12-31 1 3
2008-1-5 1 2
2008-1-20 2 2
2008-1-25 2 3
2008-1-26 1 2
2008-1-27 2 1
2008-1-30 2 5
2008-2-10 1 2
2008-2-25 2 3我想做个数据统计,2007-12-26至2008-1-25算1月份,2008-1-26至2008-2-25算2月份.
如果得到正确结果
月份 1分 2分
1 7 10
2 4 9能用SQL语句一步到位吗???
2007-12-26 1 2
2007-12-30 2 5
2007-12-31 1 3
2008-1-5 1 2
2008-1-20 2 2
2008-1-25 2 3
2008-1-26 1 2
2008-1-27 2 1
2008-1-30 2 5
2008-2-10 1 2
2008-2-25 2 3我想做个数据统计,2007-12-26至2008-1-25算1月份,2008-1-26至2008-2-25算2月份.
如果得到正确结果
月份 1分 2分
1 7 10
2 4 9能用SQL语句一步到位吗???
set @sql=''
select @sql=@sql+',sum(case 分公司 when '+rtrim(分公司)+' then 数量 else 0 end) as ['+rtrim(分公司)+'分]'
from tab
group by 分公司exec('select case when day(日期)>=26 then month(dateadd(month,1,日期)) else month(日期) end as 月份'+@sql+' from tab where 日期>=''2007-12-26'' group by case when day(日期)>=26 then month(dateadd(month,1,日期)) else month(日期) end')
select case when day(日期)>=26 then month(dateadd(month,1,日期)) else month(日期) end as 月份,
sum(case 分公司 when 1 then 数量 else 0 end) as [1分],
sum(case 分公司 when 2 then 数量 else 0 end) as [2分]
from tab
where 日期>='2007-12-26'
group by case when day(日期)>=26 then month(dateadd(month,1,日期)) else month(日期) end
create table tb(id int,date datetime,sale decimal(10,2))
insert into tb select 1,'2008-01-01',10
insert into tb select 2,'2008-01-01',10
insert into tb select 3,'2008-02-01',20
insert into tb select 4,'2008-02-01',20
insert into tb select 5,'2008-03-01',30
insert into tb select 6,'2008-03-01',30
insert into tb select 7,'2008-04-01',40
insert into tb select 8,'2008-04-01',40
insert into tb select 9,'2008-05-01',50
insert into tb select 10,'2008-05-01',50
insert into tb select 11,'2008-06-01',60
insert into tb select 12,'2008-06-01',60
insert into tb select 13,'2008-07-01',70
insert into tb select 14,'2008-07-01',70
insert into tb select 15,'2008-08-01',80
insert into tb select 16,'2008-08-01',80
insert into tb select 17,'2008-09-01',90
insert into tb select 18,'2008-09-01',90
insert into tb select 19,'2008-10-01',100
insert into tb select 20,'2008-10-01',100
insert into tb select 21,'2008-11-01',110
insert into tb select 22,'2008-11-01',110
insert into tb select 23,'2008-12-01',120
insert into tb select 24,'2008-12-01',120--静态查询
select
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='01' then sale else 0 end) as '1月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='02' then sale else 0 end) as '2月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='03' then sale else 0 end) as '3月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='04' then sale else 0 end) as '4月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='05' then sale else 0 end) as '5月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='06' then sale else 0 end) as '6月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='07' then sale else 0 end) as '7月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='08' then sale else 0 end) as '8月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='09' then sale else 0 end) as '9月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='10' then sale else 0 end) as '10月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='11' then sale else 0 end) as '11月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='12' then sale else 0 end) as '12月'
from tb
--插入2009年数据
insert into tb select 25,'2009-01-01',10
--动态查询
declare @sql varchar(max)
select @sql=isnull(@sql+',','')+'sum(case when datepart(yy,date)='''+ltrim([year])+''' and datepart(mm,date)='''+ltrim([month])+''' then sale else 0 end) as ['+ltrim([year])+'年'+ltrim([month])+'月]'
from (select distinct datepart(yy,date) as [year],datepart(mm,date) as [month] from tb)a
exec('select '+@sql+' from tb')1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
20.00 40.00 60.00 80.00 100.00 120.00 140.00 160.00 180.00 200.00 220.00 240.00
FROM Table5
group by Format(日期+IIf(Day(日期)>25,10,0),'yyyy-mm')
如果您问题已经得解决,请您及时结帖给分,以感谢帮助您的朋友。 结帖方法:点击版面右上方或右下方 <管理> ,进入页面后就可以输入密码,分别给分,结帖。
或参考:http://www.csdn.net/help/over.asp