首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sqlserver 按月份统计 出金、入金、剩余金额解决方法

2013-01-25 
sqlserver 按月份统计 出金、入金、剩余金额月份出金入金剩余金额10.000.000.0020.000.000.0030.000.000.004

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.月份

热点排行