读书人

sqlserver 按月份统计 出金、入金、剩

发布时间: 2013-01-25 15:55:30 作者: rapoo

sqlserver 按月份统计 出金、入金、剩余金额
月份 出金 入金 剩余金额
10.000.000.00
20.000.000.00
30.000.000.00
40.000.000.00
50.000.002000.00
60.000.002000.00
70.000.001000.00
80.000.001000.00
950.553569.17-1678.82
100.000.000.00
110.000.000.00
120.000.000.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.月份

读书人网 >SQL Server

热点推荐