计算某个字段在当前记录和下一条记录之间的差

描述:
  计算某个字段在当前记录和下一条记录之间的差
  例如,对于每一个员工,计算出他们的入职日期之间相差多少天

解决方案:

with dataset as (
    select 'Jack' as Name,20190101 as Hiredate from dual
    union all
    select 'Tom' as Name,20190102 as Hiredate from dual
    union all
    select 'Jimmy' as Name,20190301 as Hiredate from dual
    union all
    select 'Frank' as Name,20190701 as Hiredate from dual
    union all
    select 'John' as Name,20190109 as Hiredate from dual
)

select name, 
        hiredate, 
        next_hiredate,
        next_hiredate - hiredate as diff
from (
    select name, 
           hiredate,
           lead(hiredate)over(order by hiredate) next_hiredate
    from dataset
)

结果:

 

猜你喜欢

转载自www.cnblogs.com/shiliye/p/12361133.html