WITH calendar AS ( -- 달력 테이블을 미리 생성해 두고 사용하시면 편리합니다. -- 주말 뿐 아니라 공휴일 관리도 가능합니다. SELECT FORMAT(DATEADD(DAY, 0, '20240422'), 'yyyyMMdd') v_dt , DATEADD(DAY, 0, '20240422') d_dt , DATEPART(WEEKDAY, DATEADD(DAY, 0, '20240422')) w_dy UNION ALL SELECT FORMAT(DATEADD(DAY, 1, d_dt), 'yyyyMMdd') v_dt , DATEADD(DAY, 1, d_dt) d_dt , DATEPART(WEEKDAY, DATEADD(DAY, 1, d_dt)) w_dy FROM calendar WHERE v_dt < '20240517' ) , DailyProdPlan AS ( SELECT '20240424' SrtDate, '20240424' EndDate UNION ALL SELECT '20240424', '20240425' UNION ALL SELECT '20240425', '20240425' UNION ALL SELECT '20240425', '20240426' UNION ALL SELECT '20240426', '20240426' UNION ALL SELECT '20240426', '20240427' UNION ALL SELECT '20240427', '20240427' UNION ALL SELECT '20240427', '20240428' UNION ALL SELECT '20240428', '20240428' UNION ALL SELECT '20240428', '20240429' UNION ALL SELECT '20240429', '20240429' UNION ALL SELECT '20240430', '20240430' UNION ALL SELECT '20240430', '20240501' UNION ALL SELECT '20240501', '20240502' UNION ALL SELECT '20240502', '20240502' UNION ALL SELECT '20240503', '20240503' UNION ALL SELECT '20240503', '20240504' UNION ALL SELECT '20240504', '20240505' ) , tmp AS ( SELECT a.v_dt old_dt , b.v_dt new_dt FROM (SELECT v_dt , ROW_NUMBER() OVER(ORDER BY v_dt) rn FROM (SELECT MIN(SrtDate) SrtDate , MAX(EndDate) EndDate FROM DailyProdPlan ) a INNER JOIN calendar c ON c.v_dt BETWEEN a.SrtDate AND a.EndDate ) a INNER JOIN (SELECT v_dt , ROW_NUMBER() OVER(ORDER BY v_dt) rn FROM (SELECT DATEADD(DAY, 2, MIN(SrtDate)) SrtDate , DATEADD(DAY, 20, MAX(EndDate)) EndDate FROM DailyProdPlan ) a INNER JOIN calendar c ON c.v_dt BETWEEN a.SrtDate AND a.EndDate WHERE c.w_dy NOT IN (1, 7) ) b ON a.rn = b.rn ) SELECT a.SrtDate , a.EndDate , b.new_dt New_SrtDate , c.new_dt New_EndDate FROM DailyProdPlan a INNER JOIN tmp b ON a.SrtDate = b.old_dt INNER JOIN tmp c ON a.EndDate = c.old_dt ;