Hodustory/프로그래밍&DB

[ORACLE] 달력 출력

호두밥 2020. 8. 19. 21:55
 SELECT
  *
 FROM
   (
    SELECT 
      CASE WEEKDAY 
        WHEN '1' THEN WEEK + 1
        ELSE WEEK
      END WEEK,
      WEEKDAY,
      DY
     FROM
      (
        SELECT
          TO_NUMBER(TO_CHAR(DT, 'IW')) WEEK,
          TO_CHAR(DT, 'D') WEEKDAY,
          TO_CHAR(DT, 'FMDD') DY
        FROM
           (SELECT TO_DATE( TO_CHAR(SYSDATE, 'YY/MM')||'/'|| LPAD(LEVEL,2,0)) DT
                 ,LEVEL DAY
            FROM DUAL
            CONNECT BY LEVEL <= LAST_DAY(SYSDATE)-TRUNC(SYSDATE, 'MM')+1 
            ) 
       )
     )
PIVOT ( MIN(DY) FOR WEEKDAY IN (1,2,3,4,5,6,7) )
ORDER BY WEEK  
;

반응형