У меня есть столбец Start Date
и End Date
в одной из таблиц SQL Server.У меня есть список дат банковских праздников.Мне нужно проверить каждый диапазон дат начала и окончания в строке, чтобы проверить, сколько выходных попадает в этот диапазон и общее их количество.
В настоящее время я достигаю этого с помощью CASE WHEN
заявлений / праздничных дней, следующих за
select
a.StartDate,
a.EndDate,
(CASE when '2017-01-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-04-14' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-04-17' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-05-22' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-07-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-08-07' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-09-04' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-10-09' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-11-11' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-12-25' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2017-12-26' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-01-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-03-30' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-04-02' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-05-21' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-07-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-08-06' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-09-03' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-10-08' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-11-11' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-12-25' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2018-12-26' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-01-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-04-19' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-04-22' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-05-20' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-07-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-08-05' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-09-02' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-10-14' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-11-11' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-12-25' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2019-12-26' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-01-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-04-10' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-04-13' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-05-18' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-07-01' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-08-03' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-09-07' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-10-12' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-11-11' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-12-25' between a.StartDate and a.EndDate then 1 else 0 end)+
(CASE when '2020-12-26' between a.StartDate and a.EndDate then 1 else 0 end)
from
(select '2017-01-01' as StartDate, '2019-12-31' as EndDate) a
Есть ли лучший способ сделать это?Может ли SQL-тест для каждого диапазона дат НАЧАЛО и КОНЕЦ, сколько дат выходных пересекается в одной строке?