日期:2014-05-17  浏览次数:20679 次

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 a.月份,isnull(b.出金,0), isnull(b.入金,0), isnull(b.剩余金额 ,0)
from (
select 1 as 月份 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 12 union all
) a
left join 
(
select month(日期字段) as 月份,sum(出金) as  出金,sum(入金) as 入金,sum(剩余金额) as 剩余金额
from tb where year(期字段)=2012
grouy by month(日期字段)
) b
on a.月份=b.月份