2016-10-06 44 views
4

我需要在plan_table表中找到日期列中第一個缺失的日期。這不應該在holiday_table或它應該屬於任何週末。在列中查找第一個缺失的日期(Oracle)

holiday_table存儲所有的假期日期。

Plan_table包含日期。在這裏,我們必須找到第一個丟失日期

Plan_id  Date 
1   10/2/2016 
2   10/3/2016 
3   10/6/2016 
4   10/9/2016 
5   10/10/2016 
6   10/12/2016 
7   10/13/2016 
8   10/16/2016 

這裏的第一個丟失的日期是2016年10月4日,但此日期是否在holiday_table那麼我們必須表現出2016年10月5日或下一次出現。 。

請幫我寫一個相同的查詢。

+0

沒有鷺,PLAN_TABLE包含這些日期。 –

回答

1

可以使用LEAD分析函數這樣

select d 
from 
    (
    select 
     date + 1 as d 
    from 
    (
     select 
     date, 
     lead(date) over(order by date) as next_date 
     from 
     (
      select date from plan_table 
     union 
      select date from holliday_table 
     ) 
     order by date 
    ) 
    where 
     trunc(date) + 1 < trunc(next_date) 
    order by d 
) 
    where rownum = 1 
; 
+0

謝謝scurik。我將在週末參加。我仍然得到了我需要的東西。 –

相關問題