sellpart_id add_flag sellpart_num
1111        1        12
1111        2        10
2222        1        5
3333        2        6
现有这样一张表,add_flag表示进或者出
需按照sellpart_id 统计出最后得sellpart_num
请问如何完成
谢谢各位

解决方案 »

  1.   

    select sellpart_id,sum(case add_flag when 1 then sellpart_num else -sellpart_num end) from t_sellpart_rescods group by sellpart_id 
    已经搞定了,谢谢各位看官
      

  2.   

    又学到一点,刚才测试了下wuzhilin的,提示缺少右括号
    把case end 改为decode就可以了
    select sellpart_id,sum(decode(add_flag,1,sellpart_num,-sellpart_num) from t_sellpart_rescods group by sellpart_id
      

  3.   

    to william3033
    我那样写是没有错的,不知道你说的缺少的右括号在那里用case 比用decode功能更强一点