1. 程式人生 > >ORACLE 根據時間段取的所有 月---周

ORACLE 根據時間段取的所有 月---周

獲取日期列表:

SELECT TO_CHAR(TO_DATE('2014-10-01', 'yyyy-MM-dd') + ROWNUM - 1, 'yyyyMMdd') as daylist  
  FROM DUAL  
CONNECT BY ROWNUM <=  
           trunc(to_date('2015-06-01', 'yyyy-MM-dd') -  
                 to_date('2014-10-01', 'yyyy-MM-dd')) + 1  

獲取月份列表:

SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10', 'yyyy-MM'), ROWNUM - 1),  
               'yyyyMM') as monthlist  
  FROM DUAL  
CONNECT BY ROWNUM <=  
           months_between(to_date('2015-06', 'yyyy-MM'),  
                          to_date('2014-10', 'yyyy-MM')) + 1  

獲取周列表:   獲取自然周    以週一開始  - 週日結束

SELECT trunc(to_DATE('2018-09-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'iw') AS mon,
       trunc(to_DATE('2018-09-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'iw') + 6 AS sun
  FROM DUAL
CONNECT BY ROWNUM <=
           (trunc(to_DATE('2018-10-31', 'YYYY-MM-DD'), 'iw') + 6 -
           trunc(to_DATE('2018-09-02', 'YYYY-MM-DD'), 'iw')) / 7 + 1

獲取年份列表:

SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10', 'yyyy-MM'), (ROWNUM - 1) * 12),  
               'yyyy') as yearlist  
  FROM DUAL  
CONNECT BY ROWNUM <=  
           months_between(to_date('2015-06', 'yyyy-MM'),  
                          to_date('2014-10', 'yyyy-MM')) / 12 + 1  

分組統計,按照天、月份周和自然周、月、季度和年

當前日期為當年第幾周,幾天,幾月,幾季度;ww,iw,w區別