日期:2014-05-18  浏览次数:20405 次

统计的问题~~~写不出来,帮帮忙 。不知道能不能实现啊 。。。哎~~~
表:
deptID       datatime               money
1                   2006-03-10             100
2                   2006-03-10             200
3                   2006-03-10             300
4                   2006-03-10             400
1                   2007-02-10             1000
2                   2007-02-10             2000
3                   2007-02-10             3000
4                   2007-02-10             4000


结果:             1                             2                         3                       4                       合计
月份         2006     2007           2006   2007       2006   2007         2006   2007       2006     2007
02               0         1000             0       2000         0       3000           0       4000         0         10000
03               100       0                 200     0             300       0             400     0             1000     0


不知道能不能实现啊     。。。哎~~~

------解决方案--------------------
declare @a table(deptID int,datatime char(10),money int)
insert into @a select 1 , '2006-03-10 ' , 100
union all select 2 , '2006-03-10 ' , 200
union all select 3 , '2006-03-10 ' , 300
union all select 4 , '2006-03-10 ' , 400
union all select 1 , '2007-02-10 ' , 1000
union all select 2 , '2007-02-10 ' , 2000
union all select 3 , '2007-02-10 ' , 3000
union all select 4 , '2007-02-10 ' , 4000
select distinct substring(datatime,6,2)as yuefen,
sum(case when deptid=1 and left(datatime,4)= '2006 'then money else 0 end)a,
sum(case when deptid=1 and left(datatime,4)= '2007 'then money else 0 end)b,
sum(case when deptid=2 and left(datatime,4)= '2006 'then money else 0 end)c,
sum(case when deptid=2 and left(datatime,4)= '2007 'then money else 0 end)d,
sum(case when deptid=3 and lef