有两个表
表a                   表b
count      name       count     name
  1        小型         1       小型
  1        中行         2       中型
  2        大型         1       微型 
  2        微型         2       超大型 实现把两个表合并 要求是把name相同的合并(count相加)例表a和表b合并后
count    name
  2      小型
  3      中型
  2      大型
  3      微型
  2      超大型请求高手们帮助 谢谢了

解决方案 »

  1.   


    create table a(
    [count] int,
    name nvarchar(20)
    )insert into a values(1,'小型')
    insert into a values(1,'中型')
    insert into a values(2,'大型')
    insert into a values(2,'微型')create table b(
    [count] int,
    name nvarchar(20)
    )insert into b values(1,'小型')
    insert into b values(2,'中型')
    insert into b values(1,'微型')
    insert into b values(2,'超大型')
    select (case when a.count is null then 0 else a.count end)+(case when b.count is null then 0 else b.count end) as count,(case when a.name is null then b.name when b.name is null then a.name else a.name end) as name from a full join b on a.name=b.name
    Result:2 小型
    3 中型
    3 微型
    2 超大型
    2 大型
      

  2.   


    create table #a(count int,name varchar(50))
    create table #b(count int,name varchar(50))
    insert into #a
    select 1,'小型' union all
    select 1,'中型' union all
    select 2,'大型' union all
    select 2,'微型' 
    insert into #b
    select 1,'小型' union all
    select 2,'中型' union all
    select 1,'微型' union all
    select 2,'超大型' select sum(count) as count,name from
     (select * from #a union all select * from #b) tb group by name
    /*
    2 超大型
    2 大型
    3 微型
    2 小型
    3 中型
    */
      

  3.   


    create table #a(count int,name varchar(50))
    create table #b(count int,name varchar(50))
    insert into #a
    select 1,'小型' union all
    select 1,'中型' union all
    select 2,'大型' union all
    select 2,'微型' 
    insert into #b
    select 1,'小型' union all
    select 2,'中型' union all
    select 1,'微型' union all
    select 2,'超大型' select sum(count) as count,name from
     (select * from #a union all select * from #b) tb group by name
    /*
    2 超大型
    2 大型
    3 微型
    2 小型
    3 中型
    */
      

  4.   


    select * into temp from a
    insert into temp select * from b
    select SUM([count]) as [count],name from temp group by namedrop table temp