WITH TT(FROM_DT, TO_DT) AS( SELECT '2014-01-01', '2014-01-09' FROM DUAL UNION ALL SELECT '2014-01-12', '2014-01-18' FROM DUAL UNION ALL SELECT '2014-01-21', '2014-02-02' FROM DUAL ) SELECT DISTINCT FROM_DT, TO_DT, TO_DATE(FROM_DT,'YYYY-MM-DD')+LEVEL-1 AS DT FROM TT CONNECT BY LEVEL <= TO_DATE(TO_DT,'YYYY-MM-DD') - TO_DATE(FROM_DT,'YYYY-MM-DD') + 1 ORDER BY FROM_DT, TO_DT, DT ;
-- 그냥 1월의 일자를 모두 보고싶은 거군요? -- 테이블과는 전혀 상관이 없는 질문인 듯. SELECT ym + LEVEL - 1 dt FROM (SELECT TO_DATE('201401', 'yyyymm') ym FROM dual) CONNECT BY LEVEL <= LAST_DAY(ym) - ym + 1 ; -- 테이블을 연관 지으려면 다음과 같이... WITH z_date_test AS ( SELECT TO_DATE('20140101', 'yyyymmdd') from_dt, TO_DATE('20140109', 'yyyymmdd') to_dt FROM dual UNION ALL SELECT TO_DATE('20140112', 'yyyymmdd'), TO_DATE('20140118', 'yyyymmdd') FROM dual UNION ALL SELECT TO_DATE('20140121', 'yyyymmdd'), TO_DATE('20140202', 'yyyymmdd') FROM dual ) , calendar AS ( SELECT ym + LEVEL - 1 dt FROM (SELECT TO_DATE('201401', 'yyyymm') ym FROM dual) CONNECT BY LEVEL <= LAST_DAY(ym) - ym + 1 ) SELECT c.dt , COUNT(d.from_dt) cnt FROM calendar c , z_date_test d WHERE c.dt BETWEEN d.from_dt(+) AND d.to_dt(+) GROUP BY c.dt ORDER BY c.dt ;