다음 쿼리 작업을해야합니다 : 여기
select coalesce(sum(datediff(day,
(case when h.from_date < t.fromdate then t.fromdate else h.from_date end),
(case when t.to_date > t.todate then t.todate else h.to_date end)
), 0)
from (select @daterange1 as fromdate, @daterange2 as todate) const left outer join
holidays h
on h.from_date <= todate and h.to_date >= from_date;
는 논리이다. 첫째, 편의를 위해 상수를 상수 테이블에 넣었습니다 ("@"키에 대한 스트레스를 제한합니다). 그런 다음 남은 쿼리는 휴일 테이블에 조인됩니다 - 기간에 공휴일이없는 경우 필요합니다.
그런 다음 겹치는 기간을 비교하는 논리를 수행합니다. 공휴일의 시작이 기간 이전 인 경우 기간 시작을 시작하십시오. 끝에 똑같은. 그런 다음이 모든 기간을 합산하십시오.
"to_date"처리 방식에 따라 기간에 "1"을 추가해야 할 수도 있습니다. "to_date"는 휴일로 포함되는지 여부를 나타냅니다. 공휴일로 포함 된 경우 논리는 다음과 같습니다.
select coalesce(sum(1+datediff(day,
(case when h.from_date < t.fromdate then t.fromdate else h.from_date end),
(case when t.to_date > t.todate then t.todate else h.to_date end)
), 0)
어떻게 공휴일이 중복되는 것을 처리 하시겠습니까? – HABO