本帖最后由 lyvscf 于 2010-09-19 16:49:05 编辑

解决方案 »

  1.   

    select flag,sum(integral) as cnt from t where flag=1
    union all
    select flag,sum(abs(integral))*-1 as cnt from t where falg in (2,5)
      

  2.   

    with t1 as (select deocode(sign(integral),1,'A','B') type,integral from table)
    select type,sum(integral ) from t1 group by type
      

  3.   


    得到的是
     
       Type  sum(integral)
        A        400
     
    我想得到的是
     
       Type  sum(integral)
        A        400
        D        -200
       
      

  4.   

    SQL> select * from tb_test;
     
    ID FLAG    INTEGRAL
    -- ---- -----------
     1    0         200
     1    0         200
     1    2        -100
     1    5        -100
     
    SQL> 
    SQL> with t1 as (select decode(sign(integral),1,'A','D') type,integral from tb_test)
      2  select type,sum(integral ) from t1 group by type
      3  ;
     
    TYPE SUM(INTEGRAL)
    ---- -------------
    A              400
    D             -200
     
    SQL> 
      

  5.   

    敢问这位高手 如果表中 的flag 又有其他的 类型的 该如何处理?
      

  6.   

    按自己情况写decode,decode相当于if语句.
      

  7.   

    select 类型, sum(积分) 积分和
    (select decode(flag,0,'A',2,'D',5,'D') 类型,integral 积分
    from tb) a
    group by 类型