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
Sql 如何在Oracle表中查找月份间隔?_Sql_Oracle_Gaps And Islands - Fatal编程技术网

Sql 如何在Oracle表中查找月份间隔?

Sql 如何在Oracle表中查找月份间隔?,sql,oracle,gaps-and-islands,Sql,Oracle,Gaps And Islands,我有一个Oracle表,其中包含EmpName Char、Month_from和Month_to列Numeric。在这里,我需要找到丢失的月份间隔。在下面的样本数据中,我必须找到6月6日缺失的月份 提前谢谢 Sample Data: |-------|-----------|--------| |eName |Month_From |Month_To| |(Char) | ( Int) | ( Int) | |-------|------------|-------| |John

我有一个Oracle表,其中包含EmpName Char、Month_from和Month_to列Numeric。在这里,我需要找到丢失的月份间隔。在下面的样本数据中,我必须找到6月6日缺失的月份

提前谢谢

Sample Data:

|-------|-----------|--------|
|eName  |Month_From |Month_To|
|(Char) | ( Int)    | ( Int) |
|-------|------------|-------|
|John   |1          |2       | ( Jan to Feb)
|John   |3          |5       | ( Mar to May)
|John   |7          |8       | ( Jul to Aug)    
|-------|------------|-------|

需要找到Jun到Jun.</P> < P>只是对样本数据进行转换,可以考虑:

select to_char(to_date(lpad(t.month_from,2,'0'),'mm'),'Mon')||' to '||
       to_char(to_date(lpad(t.month_to,2,'0'),'mm'),'Mon')
  from my_table t
 where upper(t.eName) = upper('&i_eName');
关于6月至6月的问题:


但是,除了6月,它还返回9月、10月、11月、12月。对于这一点,我同意@mathguy的评论。

假设没有重叠,您可以使用lag查找缺少的月份:


这为每个间隔提供了一个范围,因为间隔可能超过一个月。

我认为您错过了OP想要返回6的点,而不是像1月到2月这样的字符串。@GordonLinoff好的,谢谢,我知道刚才的最后一句话。@GordonLinoff今天,我可以为6月到6月的部分看这个,再次感谢你。你和我们怎么知道9月9日也没有错过?如果月份可能缺失,那么是什么保证它们不会在序列的开始和/或结束时缺失,而只能在序列中缺失?感谢您的帮助。
select to_char(to_date(lpad(a1.mon,2,'0'),'mm'),'Mon')
  from ( select level mon from dual connect by level <= 12 ) a1
 where not exists ( select null
                      from my_table a2
                     where a1.mon between a2.month_from and a2.month_to
                       and upper(a2.eName) = upper('&i_eName') )
 order by mon;
select (prev_month_to + 1) as start_missing,
       (month_from - 1) as end_missing
from (select t.*, lag(month_to) over (partition by name order by month_from) as prev_month_to
      from t
     ) t
where prev_month_to <> month_from - 1;