三张表组合统计
有三张表:
表1:
TableName : A
Num UserID
1 1
10 2
20 5
表2:
TalbeName : B
Num UserID
5 1
14 5
8 7
表3:
TableName : C
Num UserID
5 1
14 9
我想把这三张表组合起来生成D表
Num UserID
11 1
10 2
34 5
8 7
14 9
sql语句怎么写
------解决方案--------------------select sum(num) as num,[UserID]
from
(
select * from A
union all
select * from B
union all
select * from C
) T
group by [UserID]
------解决方案--------------------select Num , UserID from
(
select UserID , sum(Num) as Num from A group by UserID
union all
select UserID , sum(Num) as Num from B group by UserID
union all
select UserID , sum(Num) as Num from C group by UserID
) t
order by UserID