WITH calendar_t AS ( SELECT '20180430' dt, 'N' hu_yn FROM dual UNION ALL SELECT '20180501', 'Y' FROM dual UNION ALL SELECT '20180502', 'N' FROM dual UNION ALL SELECT '20180503', 'N' FROM dual UNION ALL SELECT '20180504', 'N' FROM dual UNION ALL SELECT '20180505', 'Y' FROM dual UNION ALL SELECT '20180506', 'Y' FROM dual UNION ALL SELECT '20180507', 'Y' FROM dual UNION ALL SELECT '20180508', 'N' FROM dual ) , data_t AS ( SELECT 1 pk, '20180430083000' req_dt, TO_DATE('2018050215', 'yyyymmddhh24') end_dt FROM dual UNION ALL SELECT 2, '20180430163000', TO_DATE('2018050215', 'yyyymmddhh24') FROM dual UNION ALL SELECT 3, '20180501063000', TO_DATE('2018050115', 'yyyymmddhh24') FROM dual UNION ALL SELECT 4, '20180502063000', TO_DATE('2018050215', 'yyyymmddhh24') FROM dual ) SELECT a.pk, a.req_dt, a.end_dt , NVL(TRUNC( SUM( GREATEST(0, LEAST(edt, edt1) - GREATEST(sdt, sdt1)) *24*60 + GREATEST(0, LEAST(edt, edt2) - GREATEST(sdt, sdt2)) *24*60 ) ), 0) AS minutes FROM (SELECT pk, req_dt, end_dt , TO_DATE(req_dt, 'yyyymmddhh24miss') sdt , end_dt edt FROM data_t ) a , (SELECT dt , hu_yn , TO_DATE(dt || '09', 'yyyymmddhh24') sdt1 , TO_DATE(dt || '12', 'yyyymmddhh24') edt1 , TO_DATE(dt || '13', 'yyyymmddhh24') sdt2 , TO_DATE(dt || '18', 'yyyymmddhh24') edt2 FROM calendar_t ) b WHERE b.dt(+) >= TO_CHAR(a.sdt, 'yyyymmdd') AND b.dt(+) <= TO_CHAR(a.edt, 'yyyymmdd') AND b.hu_yn(+) = 'N' GROUP BY a.pk, a.req_dt, a.end_dt ORDER BY pk ;