sqlserver 按月份统计 出金、入金、剩余金额
月份 出金 入金 剩余金额
1 0.00 0.00 0.00
2 0.00 0.00 0.00
3 0.00 0.00 0.00
4 0.00 0.00 0.00
5 0.00 0.00 2000.00
6 0.00 0.00 2000.00
7 0.00 0.00 1000.00
8 0.00 0.00 1000.00
9 50.55 3569.17 -1678.82
10 0.00 0.00 0.00
11 0.00 0.00 0.00
12 0.00 0.00 0.00
按照这种结果统计 代码该怎么写噢?
求救各位大神。
------解决方案--------------------select month(time) as 月份 , sum(jine) as 出金,sum(jine1) as 入金 ,sum(jine1-jine) as 剩余金额from tablename group by month(time) order by month(time)
------解决方案--------------------好像是这样的
select number as 月份 ,出金, 入金, 剩余金额
from master..spt_values s
left join
(
select month(time) as 月份 ,
sum(出金) as 出金,
sum(入金) as 入金 ,
sum(剩余金额) as 剩余金额
from tablename
where year(time)=2012
group by month(time)
) t
on s.number=t.月份
where s.type='p' and number between 1 and 12