본문 바로가기

Dev::DB/Oracle

달력 SQL

WITH T AS 
(
    SELECT TO_DATE('200811','yyyymm') YM FROM DUAL
)
SELECT * 
  FROM (
        SELECT MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '1', LEVEL)) SUN,
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '2', LEVEL)) MON,
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '3', LEVEL)) TUE,
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '4', LEVEL)) WED,
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '5', LEVEL)) THU,
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '6', LEVEL)) FRI, 
               MIN (DECODE (TO_CHAR (YM + LEVEL - 1, 'd'), '7', LEVEL)) SAT
          FROM T
       CONNECT BY LEVEL <= LAST_DAY (YM) - YM + 1
         GROUP BY TRUNC (YM + LEVEL, 'iw')
         ORDER BY 7)