sql-server - 检查某些日期是否适合开始和结束日期范围以及匹配日期的总数
问题描述
我在其中一个 SQL Server 表中有一个Start Date
andEnd Date
列。我有一个银行假日日期列表。我需要测试每行的每个开始和结束日期范围,以检查有多少假期属于该范围以及这些假期的总数。
我目前正在通过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 可以为每个 START 和 END 日期范围测试一行中有多少个假日日期相交?
解决方案
推荐阅读
- java - Appium 不显示 webview 上下文,它只显示本机应用程序上下文。如何获取 webview 上下文?
- java - 最后的分析失败了。后台任务页面上提供了更多详细信息
- python - 如何获取弹性搜索索引中列(字段)中不同值的计数
- c++ - 嵌套可变参数模板
- .net - 如何在 Visual Studio 2017 专业版中启用 ASP.Net Core 2.2
- cordova - Ionicv3 使用 minSdk 最多为 16 的兼容库或将此项目的 minSdk 版本增加到至少 19,
- jquery - 如何通过生成的jsonarray动态创建多个数据表?
- dkim - 如何使用 C# .Net 使用 Chilkat 将带有 DKIM 的电子邮件发送到 BCC
- ios - 部署到 Firebase 托管时,HTML5 视频无法通过 HTTPS 在 iOS 上播放
- mysql - mysql 子查询错误 [21000][1241] 操作数应包含 1 列