Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Oracle中使用sql查找日期范围中的差距_Sql_Oracle_Datetime_Plsql_Gaps And Islands - Fatal编程技术网

在Oracle中使用sql查找日期范围中的差距

在Oracle中使用sql查找日期范围中的差距,sql,oracle,datetime,plsql,gaps-and-islands,Sql,Oracle,Datetime,Plsql,Gaps And Islands,我需要使用sql查找基本日期范围和测试范围之间的差距 SD和ED是开始日期和结束日期。A和B的所有行都在同一个表中 A的约会 B的日期 缺失间隙范围 我在这里看了一些例子 但他们不像我 select to_char(SD, 'yyyymmdd') as SD, to_char(ED, 'yyyymmdd') as ED from ( -- prepare gaps in each A after removing all B select BE

我需要使用sql查找基本日期范围和测试范围之间的差距 SD和ED是开始日期和结束日期。A和B的所有行都在同一个表中

A的约会

B的日期

缺失间隙范围

我在这里看了一些例子 但他们不像我

select 
   to_char(SD, 'yyyymmdd') as SD,
   to_char(ED, 'yyyymmdd') as ED
from
   (  -- prepare gaps in each A after removing all B
      select
         BED + 1 as SD,
         lead(BSD, 1, AED + 1) over (partition by ASD,AED order by BSD) - 1 as ED
      from
         (  -- prepare all intersections between A and B
            select AA.sd as ASD, AA.ed as AED, BB.sd as BSD, BB.ed as BED
            from AA join BB on least(AA.ed, BB.ed) >= greatest(AA.sd, BB.sd)
            union all
            select AA.sd, AA.ed, to_date('1000','yyyy'), AA.sd - 1
            from AA
         )
   )   
where SD <= ED  -- exclude empty gaps
order by 1

这太棒了。谢谢!!!!。我的数据是整数,不是数据库中的日期,而是一个表中的日期,你能在这里添加注释吗?在这里,它是非常酷的sql,但不确定它是如何运行的working@NatashaThapa-将数字转换为日期:转换为日期编号“yyyymmdd”,将日期转换为数字:转换为日期,“yyyymmdd”如果我必须更改如何处理A中的重叠,并且希望避免B中的重叠,请您添加一些注释,说明sql的每个部分正在做什么
 ID  SD            ED
 B   20130120      20130420
 B   20130601      20130830
 B   20130910      20131130
Output should be: 
the Dates that are in A but are not in B with no dates overlaps
SD           ED
20130101     20130119
20130421     20130531
20130831     20130909
select 
   to_char(SD, 'yyyymmdd') as SD,
   to_char(ED, 'yyyymmdd') as ED
from
   (  -- prepare gaps in each A after removing all B
      select
         BED + 1 as SD,
         lead(BSD, 1, AED + 1) over (partition by ASD,AED order by BSD) - 1 as ED
      from
         (  -- prepare all intersections between A and B
            select AA.sd as ASD, AA.ed as AED, BB.sd as BSD, BB.ed as BED
            from AA join BB on least(AA.ed, BB.ed) >= greatest(AA.sd, BB.sd)
            union all
            select AA.sd, AA.ed, to_date('1000','yyyy'), AA.sd - 1
            from AA
         )
   )   
where SD <= ED  -- exclude empty gaps
order by 1