我有三张表,表结构是一致的,有序号(标识列),编号,数量,金额,我想把三个表中对应编号的"数量"和"金额"列合并,生成新的同样的表,请问怎么做?
如:表1:
序号  编号  数量  金额
1     a01    61    10
2     a02    50    20
3     a03    20    30表2:
序号  编号  数量  金额
1     a01    20    23
2     a02    10    51
3     a04    15    30表3类似
生成表A:序号  编号  数量  金额
1     a01    81    33
2     a02    60    71
3     a03    20    30
4     a04    15    30
 

解决方案 »

  1.   

    select 序号,  编号 ,count( 数量 )as 数量,count( 金额) as 金额 from(select * from table1 a join table2  b on  a.编号=b.编号)c group by 编号
      

  2.   

    select 序号,  编号 ,count( 数量 )as 数量,count( 金额) as 金额 
    from (select * from 表1  as a, 表2 as b
    where  a.编号 = b.编号) as c
    group by 编号
      

  3.   


    上面好像不对
    =========================select 序号,  编号 ,sum( 数量 )as 数量,sum( 金额) as 金额 
    from (select * from 表1  union all select * from  表2 ) as c
    group by 编号
      

  4.   

    select ID=identity(int, 1, 1), tmp.编号, sum(tmp.数量) as 数量, sum(tmp.金额) as 金额 into A 
    from
    (
    select 编号, 数量, 金额 from T1
    union all 
    select 编号, 数量, 金额 from T2
    union all 
    select 编号, 数量, 金额 from T3
    )tmp group by tmp.编号
      

  5.   

    select identity(id,1), 编号,sum(数量),sum(金额)
    into newtablename
    from
    (select 编号,数量,金额 from 表1
    union
    select 编号,数量,金额 from 表2
    union
    select 编号,数量,金额 from 表3) as a
    group by 编号
      

  6.   


    create Table T1(序号 int, 编号 char(3), 数量 int, 金额 int)
    insert T1 select 1,     'a01',    61,    10
    union all select 2,     'a02',    50,    20
    union all select 3,     'a03',    20,    30create Table T2(序号 int, 编号 char(3), 数量 int, 金额 int)
    insert T2 select 1,     'a01',    20,    23
    union all select 2,     'a02',    10,    51
    union all select 3,     'a04',    15,    30create Table T3(序号 int, 编号 char(3), 数量 int, 金额 int)select ID=identity(int, 1, 1), tmp.编号, sum(tmp.数量) as 数量, sum(tmp.金额) as 金额 into A 
    from
    (
    select 编号, 数量, 金额 from T1
    union all 
    select 编号, 数量, 金额 from T2
    union all 
    select 编号, 数量, 金额 from T3
    )tmp group by tmp.编号select * from A
    --result
    ID          编号   数量          金额          
    ----------- ---- ----------- ----------- 
    1           a01  81          33
    2           a02  60          71
    3           a03  20          30
    4           a04  15          30(4 row(s) affected)