oracle查询来找出从雇用日期开始的最大体验?

zf9nrax1  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(292)

必须找到最有经验的员工。

select empno, ename, 
from emp
where max(months_between(SYSDATE,hiredate))

我的软件在sysdate上出现错误,所以请任何人验证这是否正确。

wb1gzix0

wb1gzix01#

使用 NOT EXISTS :

select e.empno, e.ename 
from emp e
where not exists (select 1 from emp where hiredate < e.hiredate)

或:

select empno, ename 
from emp 
where hiredate = (select min(hiredate) from emp)

或修改 WHERE 你的问题是这样的:

select empno, ename 
from emp 
where months_between(SYSDATE, hiredate) = (
  select max(months_between(SYSDATE, hiredate)) 
  from emp
)
rsaldnfx

rsaldnfx2#

您可以使用:

select e.*
from emp e
order by hiredate asc
fetch first 1 row only;

如果你想复制,那就添加 with ties .
以上在oracle12c+中工作。在早期版本中,您需要一个子查询:

select e.*
from (select e.*,
             row_number() over (order by hiredate) as seqnum
      from emp e
     ) e
where seqnum = 1;

相关问题