日期:2014-05-18 浏览次数:20547 次
create table #A ( id int identity(1,1), subid int, paysubid int ) create table #B ( id int identity(1,1), subid int, itemname nvarchar(50), funds int ) create table #C ( id int identity(1,1), paysubid int, payitemname nvarchar(50), payfunds int ) insert into #A select 3126,3319 union all select 3127,3320 insert into #B select 3126,'水费',7600 union all select 3126,'电费',3000 union all select 3127,'水费',1200 union all select 3127,'津贴补贴',5000 union all select 3127,'工资福利支出',2000 insert into #C select 3319 ,'水费',200 union all select 3319,'电费',300 union all select 3319 ,'水费',500 union all select 3319 ,'电费',300 union all select 3319,'办公费',1000 union all select 3320,'水费',100 union all select 3320,'津贴补贴',200 union all select 3320,'水费',500 union all select 3320,'工资福利支出',900 select c.payitemname,isnull(b.funds,0),c.payfunds,c.paysubid from ( select paysubid,payitemname,sum(payfunds)payfunds from #C group by paysubid,payitemname)c join #A a on a.paysubid=c.paysubid left join (select subid,itemname,SUM(funds)funds from #B group by subid,itemname) b on b.subid=a.subid and b.itemname=c.payitemname order by c.paysubid drop table #A drop table #B drop table #C 办公费 0 1000 3319 电费 3000 600 3319 水费 7600 700 3319 工资福利支出 2000 900 3320 津贴补贴 5000 200 3320 水费 1200 600 3320