没有那么多分了,大家谅解table很简单,俩个栏位:TIME  NUM
数据每天只有一条TIME        NUM
2011/01/01  5
2011/01/02  20
......
现在要按月份统计,不满一个月的用星期统计,比如统计2010.01.01-2010.04.18的
结果格式如下
TIME       SUM
Jan.10     152        --2010年一月的
Feb.10     93         --2010年二月的
Mar.10     45
W1101      10         --4月份不满一个月,按周统计,第一周的
W1102      26         --4月份第二周的
W1103      13
搞了半天没搞好,请教各位

解决方案 »

  1.   


    with tb as(
    select to_date('20100101','yyyymmdd') sj,233 cnt from dual union all
    select to_date('20100203','yyyymmdd'),44 from dual union all
    select to_date('20100303','yyyymmdd'),22 from dual union all
    select to_date('20100403','yyyymmdd'),34 from dual union all
    select to_date('20100503','yyyymmdd'),67 from dual union all
    select to_date('20100603','yyyymmdd'),3 from dual union all
    select to_date('20100703','yyyymmdd'),998 from dual union all
    select to_date('20100803','yyyymmdd'),412 from dual union all
    select to_date('20100903','yyyymmdd'),93 from dual union all
    select to_date('20101003','yyyymmdd'),100 from dual union all
    select to_date('20101103','yyyymmdd'),110 from dual union all
    select to_date('20101203','yyyymmdd'),2 from dual union all
    select to_date('20101221','yyyymmdd'),45 from dual union all
    select to_date('20110103','yyyymmdd'),434 from dual union all
    select to_date('20110203','yyyymmdd'),22 from dual union all
    select to_date('20110207','yyyymmdd'),34 from dual union all
    select to_date('20110303','yyyymmdd'),67 from dual)
    select to_char(sj,'mon.yy'),sum(cnt)
    from tb
    where to_char(sj,'yyyymm')<to_char(sysdate,'yyyymm')
    group by to_char(sj,'mon.yy')
    union all
    select 'W'||to_char(sj,'yy.mm.w'),sum(cnt)
    from tb
    where to_char(sj,'yyyymm')>=to_char(sysdate,'yyyymm')
    group by to_char(sj,'yy.mm.w')
    TO_CHAR(S   SUM(CNT)
    --------- ----------
    apr.10            34
    may.10            67
    aug.10           412
    oct.10           100
    jul.10           998
    jan.11           434
    jan.10           233
    nov.10           110
    dec.10            47
    sep.10            93
    mar.10            22
    feb.11            56
    feb.10            44
    jun.10             3
    W11.03.1          67