在论坛中出现的比较难的sql问题:33(递归 连续日期问题 )

ε祈祈猫儿з 提交于 2019-12-11 00:40:26
原文:在论坛中出现的比较难的sql问题:33(递归 连续日期问题 )

最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。 

MS-SQL取连续日期问题  
http://bbs.csdn.net/topics/390635235?page=1#post-395995697


我现在需要得出表A中所有FBeginDate和FEndDate字段间的日期


下面是我的解法,使用了递归查询:

drop table tb create table tb(FBeginDate datetime,FEndDate datetime) insert into tbselect '2010-10-01','2010-10-01'union all select '2010-10-01','2010-10-07'union all select '2011-01-30','2011-02-12'  ;with tas(select 1 as numberunion allselect number + 1from twhere t.number < 100) select tb.FBeginDate,       tb.FEndDate,       dateadd(day,t.number-1,FBeginDate) as '两个日期之间的天'from tbinner join t        on datediff(day,FBeginDate,FEndDate) +1 >= t.numberorder by tb.FBeginDate,         '两个日期之间的天'         /*FBeginDate	            FEndDate	            两个日期之间的天2010-10-01 00:00:00.000	2010-10-01 00:00:00.000	2010-10-01 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-01 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-02 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-03 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-04 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-05 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-06 00:00:00.0002010-10-01 00:00:00.000	2010-10-07 00:00:00.000	2010-10-07 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-01-30 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-01-31 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-01 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-02 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-03 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-04 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-05 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-06 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-07 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-08 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-09 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-10 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-11 00:00:00.0002011-01-30 00:00:00.000	2011-02-12 00:00:00.000	2011-02-12 00:00:00.000*/ 


发布了416 篇原创文章 · 获赞 135 · 访问量 94万+
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!