求助:sql查询两张表统计的语句,感谢

2024-12-20 05:48:50
推荐回答(4个)
回答1:

select t.id,sum(t.2012 ),sum(t.2013) from (
select t1.id, t1.2012 as "2013"
from t1
union all
select t2.id, t2.2013 as "2013" from t2) t
group t.id

回答2:

select
ids.id, t1.[2012], t2.[2013]

from (select id from t1 union select id from t2) as ids
left join t1 on t1.id=ids.id
left join t2 on t2.id=ids.id

回答3:

select t3.id,sum([t3.2012]),sum([t3.2013] )from
(select id,[2012],'' as [2013] from t1
union all
select id,'' as [2012],[2013] from t2) as t3
group by t3.id

回答4:

select ISNULL(a.id,b.id),ISNULL([a.2012],''),ISNULL([b.2013],'') from #t1 a full join #t2 b on a.id=b.id