create table test
(
 usName varchar2(10),
 age number(18,2),
 usmey  number(18,2)
);
insert into test values ('aa',21,10);
insert into test values ('bb',22,10);
insert into test values ('cc',23,10);
insert into test values ('aa',21,10);
insert into test values ('bb',22,10);
insert into test values ('cc',23,10);
insert into test values ('dd',21,10);select usName,sum(usmey) from test  group by rollup(usName);drop table test;结果
1 aa 20
2 bb 20
3 cc 20
4 dd 10
5 70我想总数结果里面去掉dd的值去掉,总数变为60,各位有何高招!
1 aa 20
2 bb 20
3 cc 20
4 dd 10
5 60

解决方案 »

  1.   

    只想到了union
    SQL> select usName,sum(usmey) from test  group by usname
      2  union
      3  select null,sum(usmey) from test where usname<>'dd'
      4  ;
     
    USNAME     SUM(USMEY)
    ---------- ----------
    aa                 20
    bb                 20
    cc                 20
    dd                 10
                       60
     
    SQL> 
      

  2.   

    我也试过这样,但能不能不用union,因为实际的计算量很大,这样又等于重复计算了一次!
      

  3.   


    --如果你去掉dd是因为dd只有一条记录的话这样试试
    SQL> select usName,sum(usmey) from test group by rollup(usName,usmey) having(count(usName))>1;USNAME     SUM(USMEY)
    ---------- ----------
    aa                 20
    aa                 20
    bb                 20
    bb                 20
    cc                 20
    cc                 20
                       70
      

  4.   


    SQL> edi
    已写入 file afiedt.buf  1  select usName,
      2  decode(grouping(usName),1,sum(usmey)-(select  usmey from test where usName='dd'),sum(usmey)) sum_usmey
      3* from test  group by rollup(usName)
    SQL> /USNAME      SUM_USMEY
    ---------- ----------
    aa                 20
    bb                 20
    cc                 20
    dd                 10
                       60SQL> 
      

  5.   

    弱弱的问一句:
    为什么不直接:
    select usName,sum(usmey) from test where usName<>'dd' group by rollup(usName);啊!
      

  6.   

    SQL> select usName,sum(usmey) from test where usname<>'dd'  group by rollup(usname)
      2  union
      3  select 'dd',sum(usmey) from test where usname='dd'
      4  ;
     
    USNAME     SUM(USMEY)
    ---------- ----------
    aa                 20
    bb                 20
    cc                 20
    dd                 10
                       60
     
    SQL> 如果usname上有索引的话可以试试这个
      

  7.   

     sql>select usName,sum(usmey) from test where usname<>'dd'  group by rollup(usname)
     union
     select 'dd',sum(usmey) from test where usname='dd'
        ;
      

  8.   

    decode(grouping(usName),1,sum(usmey)-(select  usmey from test where usName='dd')
    这一句是什么意思啊 求大哥们给我解释一下!
      

  9.   

    GROUPING Function
    GROUPING handles these problems. Using a single column as its argument, GROUPING returns 1 when it encounters a NULL value created by a ROLLUP or CUBE operation. That is, if the NULL indicates the row is a subtotal, GROUPING returns a 1. Any other type of value, including a stored NULL, returns a 0.GROUPING appears in the selection list portion of a SELECT statement. Its form is:SELECT …  [GROUPING(dimension_column)…]  … 
      GROUP BY …    {CUBE | ROLLUP| GROUPING SETS}  (dimension_column)
      

  10.   

    rollup的合计返回的合计项是null,再用grouping转成1,所以grouping是1的时候减去'dd'的合计值.
      

  11.   


    ---
    select  usname,
    case when grouping(usname)=1 then sum(usmey)-(select usmey from test where usname='dd')else sum(usmey) end sum_usmey
    from test group by rollup (usname)