列出表的两个日期列之间的所有日期
我的表 PRODUCT
有 3 列:
Product_ID
INTRODUCED_DATE
WITHDRAWAL_DATE
我需要从此表创建一个派生表 PRODUCT_ALL_DATES
,其中列出了产品处于活动状态的所有日期。日期范围为 INTRODUCED_DATE
(开始日期)和 WITHDRAWAL_DATE
(结束日期)
如何在 SQL Server 中实现此目的?我已在附图中指出了示例输出:
谢谢!
My table PRODUCT
has 3 columns:
Product_ID
INTRODUCED_DATE
WITHDRAWAL_DATE
I need to create a derived table PRODUCT_ALL_DATES
from this table that list all the dates that a Product was active.The Date ranges are INTRODUCED_DATE
(Start Date) and WITHDRAWAL_DATE
(End Date)
How can I achieve this in SQL Server?I have indicated the sample output in the attached image:
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以想到两种方法来实现此目的
显而易见的问题是你为什么要这样做?以目前的数据形式无法解决的问题是什么
I can think of 2 ways to achieve this
The obvious question is why do you want to do this? what is the problem that cannot be solved with data being in its current form
这是您的查询的答案,
我用光标尝试过这个。它运行良好。
Here is the answer of your query,
i tried this by using Cursor. It is working fine.
根据日期的总范围,这应该有效。如果总体日期范围较大,您可能需要调整 MAX 递归。
Depending on the total ranges of dates, this should work. You might need to adjust the MAX recursion if you have large range of dates overall.