如何使用开始日期和结束日期(SQL)获取每个日期一行?
我是 SQL 新手,想知道是否有人可以帮助我提出查询。
我需要从这里开始:
对此:
基本上,我有一个包含一些类标签及其开始和结束日期的表格。我想在开始日期和结束日期之间每个日期有一行。我尝试在 SQL 中查找一些日期函数,并且知道我可能必须在某些时候使用 DATEADD,在开始日期中添加天数,直到到达结束日期。除此之外,我很困难,希望得到任何帮助!谢谢
I'm new to SQL and was wondering if someone can help me come up with a query.
I need to go from this:
To this:
Basically, I have a table with some class labels and their start and end dates. I would like to have one row per date between the start and end dates. I have tried looking up some date functions in SQL and know I probably have to use DATEADD at some point, to add days to the start date until we reach the end date. Beyond this I am pretty stuck and would appreciate any help with this! Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用递归 cte 来构建日期序列并将其连接到您的类表中。类似 -
这是一个 db<>fiddle
I would use a recursive cte to build up the date sequence and join it to your class table. Something like -
Here's a db<>fiddle