如果数据是这样的:id      qty
-------------
A        2
A        -2
A        5
B        5
B        -5
C        3
C        -1
C        -1
-------------因为B组qty合计为零,需要过滤掉它,最终想得到这样的结果:
id      qty
-------------
A        2
A        -2
A        5
C        3
C        -1
C        -1
-------------
怎样写sql语句呢?

解决方案 »

  1.   

    group by id having sum(qty)<>0
      

  2.   

    不对啊,qty不在聚合函数中,加上也不对
      

  3.   

    declare @t table(id varchar(2) ,     qty int) 
    ------------- 
    insert @t select ' A',        2 
    insert @t select ' A' ,       -2 
    insert @t select ' A' ,       5 
    insert @t select ' B',        5 
    insert @t select ' B'  ,      -5 
    insert @t select ' C'  ,      3 
    insert @t select ' C'  ,      -1 
    insert @t select  'C'  ,      -1 
    select ID,qty from @t where id in(select Id  from @t group by ID having sum(qty)<>0 )