select model,sum(s1) as s1,sum(tx) as tx
from(
select isnull(a.model,b.model) as model
       a.s1,b.tx
from A表 a right B表 b on a.model=b.model
) a order by model

解决方案 »

  1.   

    或:
    select isnull(a.model,b.model) as model
           a.s1,b.tx
    from (select model,sum(s1) as s1 from A表 group by model) a right B表 b on a.model=b.model
      

  2.   

    谢谢zjcxc(邹建),第一组比较难懂,第二组就好懂些。
    请问isnull(a.model,b.model)是什么意思?
    能把a333  0   c5423显示出来吗?(因为我没有在单位,没有办法试)
      

  3.   

    上面的写得还是有点小错误,更正如下:select isnull(a.model,b.model) as model
           ,isnull(a.s1,0),b.tx
    from (select model,sum(s1) as s1 from A表 group by model) a right join B表 b on a.model=b.model因为我用的是右连接,在A表中是没有a333  0   c5423的
    所以用isnull的目的就是让它在A表中没有时,显示B表的model
    第二个isnull是没有此记录时显示为0,而不是原来的NULL
      

  4.   

    下面是我做的数据测试--创建数据测试环境
    declare @a表 table(model varchar(10),s1 int)
    insert into @a表
    select 'a100',100
    union all select 'a222',200
    union all select 'a100',115declare @b表 table(model varchar(10),tx varchar(10))
    insert into @b表
    select 'a100','a1818'
    union all select 'a222','b2323'
    union all select 'a333','c5423'
    --显示结果
    select isnull(a.model,b.model) as model
           ,isnull(a.s1,0),b.tx
    from (select model,sum(s1) as s1 from @A表 group by model) a 
    right join @B表 b on a.model=b.model
      

  5.   

    这里没加字段名,isnull(a.s1,0), 要加上:,isnull(a.s1,0) as s1,
    上面的执行结果:model      s1          tx         
    ---------- ----------- ---------- 
    a100       215         a1818
    a222       200         b2323
    a333       0           c5423(所影响的行数为 3 行)
      

  6.   

    select model,isnull((select sum(sl) from a where model=b.model),0) sl,tx from b
      

  7.   

    zjcxc,sorry,我原先没有注意到是20分的,现在只能给20分了