oracle 求连续日期的起始日期和终止日期

来源:互联网 发布:知乎 阑夕 老司机 编辑:程序博客网 时间:2024/05/16 01:42
 194人阅读 评论(0) 收藏 举报

[c-sharp:nogutter] view plaincopy
  1. //数据  
  2. 1 2011-2-1  
  3. 1 2011-2-2  
  4. 1 2011-2-3  
  5. 1 2011-2-11  
  6. 1 2011-2-12  
  7. 1 2011-2-13  
  8. //结果  
  9. 1 2001-2-1 2011-2-3  
  10. 1 2011-3-11 2011-2-13  
  11. //解法:  
  12. with t as (  
  13.      select 1 id, date'2011-2-1'  mydate from dual union all  
  14.      select 1 id, date'2011-2-2'  mydate from dual union all  
  15.      select 1 id, date'2011-2-3'  mydate from dual union all  
  16.      select 1 id, date'2011-2-11' mydate from dual union all  
  17.      select 1 id, date'2011-2-12' mydate from dual union all  
  18.      select 1 id, date'2011-2-13' mydate from dual)  
  19.     
  20. SELECT id, MIN(mydate), MAX(mydate)  
  21. FROM t  
  22. START WITH NOT EXISTS (SELECT 1 FROM t b WHERE b.mydate = t.mydate - 1)  
  23. CONNECT BY PRIOR t.mydate = t.mydate - 1  
  24. GROUP BY rownum - LEVEL, id;  
 

原帖:

http://topic.csdn.net/u/20110602/09/02d22133-5427-4758-90fd-4f1519679994.html?95544

关于start with connect by:

[1]http://psoug.org/reference/connectby.html

[2]http://blog.csdn.net/BOBO12082119/archive/2011/03/19/6261344.aspx

connect by:

[1]http://www.dba-oracle.com/t_connect_by.htm