首先把下一条记录的雇佣日期作为当前行,这需要用到 lead over() 分析函数
select e.deptno,e.ename,e.hiredate, lead(e.hiredate) over(order by e.hiredate) next_nd
from emp e
where e.deptno =10
然后再作相减
select t.deptno,t.ename,t.hiredate,t.next_nd -t.hiredate days from (
select e.deptno,e.ename,e.hiredate, lead(e.hiredate) over(order by e.hiredate) next_nd
from emp e
where e.deptno =10
) t

还有一个 和lead 相对应的函数 ,log 函数 请看如下对比
select e.deptno,e.ename,e.hiredate, lead(e.hiredate) over(order by e.hiredate) next_nd,
lag(e.hiredate) over(order by e.hiredate) lag_nd
from emp e
where e.deptno =10;
一个是后一个往前一条,一个是前一条的日期放在后天一条上
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31369373/viewspace-2127133/
,如需转载,请注明出处,否则将追究法律责任。