Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

sql - 显示最后一名被录用的员工(show the last employee who was hired)

I need to find most recent hired employees for example i have table

(我需要找到最近雇用的员工,例如我有桌子)

INPUT

(输入)

------------------------
|   name  |  hire_date |
------------------------
| Michael | 11-JAN-2010|
| Eugene  | 20-DEC-2018|
------------------------

Need to add third column with '*' in one line with last hired employeer

(需要与最后雇用的雇员在一行中添加带有“ *”的第三列)

OUTPUT

(输出值)

----------------------------------
|   name  |  hire_date | maxdate |
----------------------------------
| Michael | 11-JAN-2010|         |
| Eugene  | 20-DEC-2018|    *    |
----------------------------------
  ask by Женя Касьян translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the most recent version of Oracle, use order by and fetch :

(在Oracle的最新版本中,使用order byfetch :)

select e.*
from employees e
order by hire_date desc
fetch first 1 row only;

In earlier versions, this is often implemented using a subquery:

(在早期版本中,通常使用子查询来实现:)

select e.*
from (select e.*
      from employees e
      order by hire_date desc
    ) 
where rownum = 1;

Both of these return only one row -- your question is in the singular.

(两者都只返回一行-您的问题是单数形式。)

There are variations that return all the most recent employees if there are duplicates.

(如果有重复,有一些变体会返回所有最近的雇员。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...