日期:2014-05-16  浏览次数:20879 次

ORACLE 比较相邻现行的日期范围是否重合
本帖最后由 l270545504 于 2014-05-04 11:16:20 编辑

如图片所示
每条记录有一个开始日期 和 一个结束日期    这两个日期就形成一个日期范围

想比较ITEM相同的两条记录的日期范围是否有重合或者连接不上的

重合的情况: 

                                开始时间             结束时间
第一条记录          2014-6-5              2014-6-9
第二条记录           2014-6-9            2014-6-20    这里2014-6-9重合了
------解决方案--------------------
建议楼主用用分析函数lead,把第二行的 开始时间"2014-6-9",作为一个列偏移到第一行中。然后,就可以用结束时间和偏移的开始时间继续比较了。     
------解决方案--------------------

select t1.num,t1.bsh01,t2.bsh01 from (select rownum as num,bshdate,bsh42,bsh01 from cshfjnsz.bsh_file
where bsh42 is not null) t1,(select rownum as num,bshdate,bsh42,bsh01 from cshfjnsz.bsh_file
where bsh42 is not null) t2
where t1.bsh42 between t2.bshdate and t2.bsh42
and t1.num<100
order by t1.num
這個是我查詢的,日期重合的太多(bshdate,bsh42),加了個限制and t1.num<100
,你可以換成你的item。