求一SQL。
要求,二表中的数据的SUM
表一
a,b,c,d
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
表二
q,w,e,r
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
要在一个SQL中求出sum(a),SUM(B),SUM(Q),SUM(E)
要求,二表中的数据的SUM
表一
a,b,c,d
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
表二
q,w,e,r
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
要在一个SQL中求出sum(a),SUM(B),SUM(Q),SUM(E)
union all
select 'b' as title,sum(b) as total from 表一
union all
select 'q' as title,sum(q) as total from 表2
union all
select 'e' as title,sum(e) as total from 表2
declare @t2 table (q int,w int,e int,r int)insert into @t1
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4insert into @t2
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4 union all
select 1,2,3,4--语句
select sum(a) as [sum(a)],SUM(B) as [SUM(B)],SUM(Q) as [SUM(Q)],SUM(E) as [SUM(E)] from @t1 full outer join @t2 on 1<>1--结果
sum(a) SUM(B) SUM(Q) SUM(E)
----------- ----------- ----------- -----------
5 10 5 15(所影响的行数为 1 行)
a,b,c,d
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
表二
q,w,e,r
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4
1,2,3,4select sum(a) ,sum(b) from 表一 left join select sum(q) q , sum(e) , e from 表二
select sum(a) ,sum(b),(select sum(q) , sum(e) from 表二) from 表一