sql problem more difficult to appear in the forum: 33 (recursive continuous problems with dates)

Original: SQL problem more difficult to appear in the forum: 33 (recursive continuous problems with dates)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea. 

MS-SQL problems take consecutive days  
http://bbs.csdn.net/topics/390635235?page=1#post-395995697


I now need to come to the table A date between all FBeginDate and FEndDate field


Here is my solution, using a recursive query:


    
    
  1. drop table tb
  2. create table tb(FBeginDate datetime,FEndDate datetime)
  3. insert into tb
  4. select '2010-10-01', '2010-10-01'
  5. union all select '2010-10-01', '2010-10-07'
  6. union all select '2011-01-30', '2011-02-12'
  7. ;with t
  8. as
  9. (
  10. select 1 as number
  11. union all
  12. select number + 1
  13. from t
  14. where t.number < 100
  15. )
  16. select tb.FBeginDate,
  17. tb.FEndDate,
  18. dateadd( day,t.number -1,FBeginDate) as '两个日期之间的天'
  19. from tb
  20. inner join t
  21. on datediff( day,FBeginDate,FEndDate) + 1 >= t.number
  22. order by tb.FBeginDate,
  23. '两个日期之间的天'
  24. /*
  25. FBeginDate FEndDate 两个日期之间的天
  26. 2010-10-01 00:00:00.000 2010-10-01 00:00:00.000 2010-10-01 00:00:00.000
  27. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-01 00:00:00.000
  28. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-02 00:00:00.000
  29. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-03 00:00:00.000
  30. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-04 00:00:00.000
  31. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-05 00:00:00.000
  32. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-06 00:00:00.000
  33. 2010-10-01 00:00:00.000 2010-10-07 00:00:00.000 2010-10-07 00:00:00.000
  34. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-01-30 00:00:00.000
  35. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-01-31 00:00:00.000
  36. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-01 00:00:00.000
  37. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-02 00:00:00.000
  38. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-03 00:00:00.000
  39. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-04 00:00:00.000
  40. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-05 00:00:00.000
  41. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-06 00:00:00.000
  42. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-07 00:00:00.000
  43. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-08 00:00:00.000
  44. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-09 00:00:00.000
  45. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-10 00:00:00.000
  46. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-11 00:00:00.000
  47. 2011-01-30 00:00:00.000 2011-02-12 00:00:00.000 2011-02-12 00:00:00.000
  48. */


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020063.html