sql - SQL:使用移位获取两个日期之间的所有日期
问题描述
我有一个包含“startbooking”和“endbooking”列的表格我还有其他内容包含转变:
id |start |end
M | 06:00 |14:00
T | 14:00 |20:00
N | 20:00 |06:00
所以,我需要通过班次获取开始预订和结束预订之间的所有日期
例子:
startbooking: 01/05/2015 12:00 endbooking: 02/05/2015 16:00
结果:
01/05/2015 |M
01/05/2015 |T
01/05/2015 |N
02/05/2015 |M
02/05/2015 |T
解决方案
也许您需要使用join进行递归: cte
cross
with t as (
select startdt, enddt
from table
union all
select dateadd(day, 1, startdt), enddt
from t
where startdt < enddt
)
select t.startdt, sft.id
from t cross join (select distinct id from shifttable) sft
option (maxrecursion 0);
这是一个演示。
推荐阅读
- javascript - 如何为我的函数访问数组中的属性?
- c# - Entity Framework 6 数据库优先 - 多对多:将其他模型中的项目添加到我的视图
- javascript - 如何在我的 REST 服务中获取调用应用程序的 URL?
- c# - 如何处理数据库查询中存储在集合中的大量数据并以特定格式打印到richtextbox?
- google-drive-api - Google Drive v3 - 如何检查文件夹是否在根目录中?
- kubernetes - 为什么我们有 NodePort 的 2000 端口
- c# - Visual Studio 2019 中的单元测试项目模板仅适用于 .NET Framework 是否重要?
- asp.net-core-mvc - 我想实现 DevExpress Grid 在绑定保存按钮时遇到问题
- django - e.message_dict 在测试文件中访问时不断抛出异常
- unity3d - 检测 ARcore 中任意位置的点