select mc,s=sum(s) from
(select mc,s from A
union all
 select mc,-s from B)t
group by mc

解决方案 »

  1.   

    或者:select mc,
           s=s-(select sum(s) from B where mc=a.mc)
    from A
      

  2.   

    declare @t table(mc char(10),s int)
    insert into @t select 'A',1200 union all
                   select 'B',1080declare @a table(mc char(10),s int)
    insert into @a select 'A',500 union all
                   select 'A',200 union all
               select 'B',108 union all
                   select 'B',508 union all
                   select 'B',208select a.mc,(a.s-b.s) as 差额 from @t a,(select mc,sum(s) as s from @a group by mc) b where a.mc=b.mc