下一个到达日期之间的间隔

时间:2019-05-20 14:58:05

标签: sql oracle

我具有如下表结构,现在我想计算第二列下一个到达日期之间的间隔

表结构

Calendar    |NEXT DAY    

15-07-1997   15-07-1997    
16-07-1997                  
17-07-1997                 
18-07-1997   18-07-1997     
19-07-1997                 
20-07-1997                 
21-07-1997                 
22-07-1997                 
23-07-1997                 
24-07-1997                 
25-07-1997                 
26-07-1997    26-07-1997    

预期结果

Calendar   |NEXT DAY    |DIFFDATE|

15-07-1997   15-07-1997    0
16-07-199                  2
17-07-1997                 1
18-07-1997   18-07-1997    0    
19-07-1997                 7
20-07-1997                 6
21-07-1997                 5
22-07-1997                 4
23-07-1997                 3
24-07-1997                 2
25-07-1997                 1
26-07-1997    26-07-1997   0

1 个答案:

答案 0 :(得分:1)

您可以使用lead(ignore nulls)

select t.*,
       (coalesce(next_day,
                 lead(next_day ignore nulls) over (order by calendar)
                ) - calendar
       ) as diffdate
from t;