求一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)

解决方案 »

  1.   

    select 'a' as title,sum(a) as total from 表一
    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
      

  2.   

    declare @t1 table (a int,b int,c int,d int)
    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 行)
      

  3.   

    表一
    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 表二
      

  4.   

    不太懂lz的意思
    select sum(a) ,sum(b),(select sum(q) , sum(e) from 表二) from 表一