ITPub博客

首页 > 数据库 > Oracle > 确定当前记录和下一条记录的之间相差的天数

确定当前记录和下一条记录的之间相差的天数

原创 Oracle 作者:linmenglan1 时间:2016-10-26 11:12:47 0 删除 编辑
首先把下一条记录的雇佣日期作为当前行,这需要用到 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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-07-03

  • 博文量
    85
  • 访问量
    157917