有一表,其时间类型如          时间                         数
        2006/01/01                     xxx.xx        2006/01/02                     xxx.xx        2006/01/03                     xxx.xx        2006/01/04                     xxx.xx
           ....                       ...
统计结果要如下:
          期间                         sum数
   2006/01/01(星期一)~2006/01/07(星期日)        xxx.xx
按自然的星期来统计

解决方案 »

  1.   

    SELECT trunc(时间,'IW'),sum(score) FROM tab group by trunc(时间,'IW');
    IW是国际标准的每个新奇第一天.
    如果要返还你的要求,只要在再加上trunc(时间,'IW')+6即可. 
    变为
      select to_char(trunc(时间,'IW'),'yyyy/mm/dd')||'(星期一)~'||to_char(trunc(时间,'IW')+6,'yyyy/mm/dd')||'(星期日)',sum(score) from tab group by to_char(trunc(时间,'IW'),'yyyy/mm/dd')||'(星期一)~'||to_char(trunc(时间,'IW')+6,'yyyy/mm/dd')||'(星期日)'
      常看sql reference即可解决类似问题.
      

  2.   

    select trunc(sysdate,'IW') from dual;
    TRUNC(SYSD
    ----------
    20-11月-06哈哈,学习!
      

  3.   

    to:dobetterthatnthink(如果你没有那么多的选择)
    你的方法试过了,好用!学习!按照你的思路,稍微改了点,容易懂一些:
    select distinct to_char(trunc(to_date(t.时间), 'IW'), 'yyyy/mm/dd') ||
                    '(星期一)~' ||
                    to_char(trunc(to_date(t.时间), 'IW') + 6, 'yyyy/mm/dd') ||
                    '(星期日)' as dd,
                    count(*) over(partition by trunc(to_date(t.时间), 'IW')) cnt
     from 表 t
     order by 1
      

  4.   

    修改一下sum、别名:
    select distinct to_char(trunc(to_date(t.时间), 'IW'), 'yyyy/mm/dd') ||
                    '(星期一)~' ||
                    to_char(trunc(to_date(t.时间), 'IW') + 6, 'yyyy/mm/dd') ||
                    '(星期日)' as 期间,
                    sum(t.数) over(partition by trunc(to_date(t.时间), 'IW')) as sum数
     from 表 t
     order by 1注意:
     一定要用‘distinct’。
      

  5.   

    SELECT TRUNC(时间 -1,'DAY') +1 "W1", sum(数) 
    FROM tab 
    group by TRUNC(时间 -1,'DAY') +1 ;