create table  test (groupp varchar2(20),file_no varchar2(20),value1 number,value2 number);
  
  insert into test values ('a', '文号1', 500,200);
  insert into test values ('a', '文号1', 500,100);
  insert into test values ('a', '文号1', 500,400);
  insert into test values ('a', '文号2', 700,400);
  insert into test values ('a', '文号2', 500,400);
  insert into test values ('a', '文号2', 600,400);
  insert into test values ('a', '文号2', 500,400);
  insert into test values ('b', '文号1', 500,55);
  insert into test values ('b', '文号1', 600,400);
  insert into test values ('b', '文号1', 700,88);
  insert into test values ('b', '文号1', 300,400);
  insert into test values ('b', '文号1', 400,55);
  insert into test values ('b', '文号2', 100,400);
  insert into test values ('b', '文号2', 100,400);
  insert into test values ('b', '文号3', 600,23);
  insert into test values ('b', '文号3', 700,33);
  insert into test values ('b', '文号3', 300,65);
  commit;
 /*
  要求:首先按照groupp分组,对于相同的file_no,如果value1值相同则此file_no的value1的汇总取值取一个,否则求和
  例如按照a分组,文号1时value1 取值为500,文号2时value1取值为700+500+600+500=2300 那么a的value1总值即为2800
   按b分组,文号1时value1取值为500+600+700+300+400=2500,文号2时value1取值为:100,文号3时value1为600+700+300=1600 那么b的value总值为4200;
  
  对于value2 无此规则 直接按groupp汇总即可
*/

解决方案 »

  1.   

    如果同一fileno下value1全部相同,则取其一,非全部相同的就全部相加?select groupp,sum(sum_value1),sum(sum_value2)
    from(
      select groupp,file_no,
        case when count(distinct value1)=1 then max(value1) else sum(value1) end sum_value1,
        sum(value2) sum_value2
      from test
      group by groupp,file_no)
    group by groupp;
      

  2.   

    select groupp,file_no,sum(value1),value2 from( 
    select groupp,file_no,value1,value2 from(
    select groupp,file_no,
    value1,nvl(lag(value1) over(partition by groupp,file_no order by groupp,file_no),0) value3,
    sum(value2) over(partition by groupp) value2 from test
    ) where value1 != value3 group by groupp,file_no,value1,value2
    ) group by groupp,file_no,value2