本帖最后由 cgx117 于 2010-08-31 11:30:57 编辑

解决方案 »

  1.   


    drop table test;
    create table test (sf varchar2(20), nf number, yf number, sj1 number, sj2 number);
    insert into test values ('XX省', 2000, 1, 1, 2);
    insert into test values ('XX省', 2000, 2, 2, 2);
    insert into test values ('XX省', 2001, 8, 3, 3);
    commit;select sf, nf, to_char(trunc((yf - 1) / 3) + 1) || '季度',sum(sj1), sum(sj2)
    from test
    group by sf, nf, trunc((yf - 1) / 3) + 1;
    SF                                     NF JD           SUM(SJ1)   SUM(SJ2)
    ------------------------------ ---------- ---------- ---------- ----------
    XX省                                  2000 1季度                 3          4
    XX省                                  2001 3季度                 3          3select sf, nf, decode(to_char(trunc((yf - 1) / 6) + 1),1,'上半年','下半年'),sum(sj1), sum(sj2)
    from test
    group by sf, nf, trunc((yf - 1) / 6) + 1;
    SF                                     NF BN           SUM(SJ1)   SUM(SJ2)
    ------------------------------ ---------- ---------- ---------- ----------
    XX省                                  2001 下半年                 3          3
    XX省                                  2000 上半年                 3          4
      

  2.   

    select 省份, 年份,季度,sum(数据1),sum(数据2) from (select 省份, 年份, 月份, 数据1, 数据2,decode(月份,1,'1季度',2,'1季度',3,'1季度',4,'2季度',5,'2季度',6,'2季度',7,'3季度',8,'3季度',9,'3季度','4季度') 季度 from data_t group by 省份, 年份,季度)
    union all 
    select 省份, 年份,日期,sum(数据1),sum(数据2) from (select 省份, 年份, 月份, 数据1, 数据2,decode(sign(月份-6),1,'下半年','上半年') 日期 from data_t group by 省份, 年份,日期)