select 货物名称,sum(case 入库方式 when '正常入库' then 数量 else  -数量 end)
group by 货物名称

解决方案 »

  1.   

    select 货物名称,数量=sum(case 入库方式 when '冲销' then (-1)*数量 else  数量 end) 
    from tb group by 货物名称
      

  2.   

    回复人: friendliu(无为) ( ) 信誉:100  和
     回复人: hdhai9451(※★山,快馬加鞭未下鞍...☆※) ( ) 信誉:100  
    的都正确create table hwb
    (货物名称 varchar (5), 数量 decimal (12,2), 入库方式 varchar (8))insert into hwb 
    select 'A',5,'正常入库' union
    select 'A',2,'冲销' union
    select 'B',2,'正常入库'
    select 货物名称,
    sum(case when 入库方式='正常入库' then 数量 else -数量 end ) as 数量
    from hwb
    group by 货物名称
    select 货物名称,数量=sum(case 入库方式 when '冲销' then (-1)*数量 else  数量 end) 
    from hwb group by 货物名称货物名称    数量
    -------    ----
    A    3.00
    B    2.00
      

  3.   

    select 货物名称,sum(case 入库方式 when '正常入库' then 数量 else  -数量 end) as 总数量
    group by 货物名称