sql中的日期问题 - oracle

时间:2018-04-26 07:21:17

标签: sql oracle

我想找出最近加入该组织的员工的姓名。

数据:

emp     Date of joining

neil    31-dec-2010
tom     31-dec-2008
fred    31-dec-2011
scott   31-dec-2011
james   31-dec-2010
shane   31-dec-2011
brendon 31-dec-2010
kane    31-dec-2009
chris   31-dec-2010
matthew 31-dec-2011

预期产出:

emp     Date of Joining

fred    31-dec-2011
scott   31-dec-2011
shane   31-dec-2011
matthew 31-dec-2011

请帮忙。提前致谢。

5 个答案:

答案 0 :(得分:5)

根据你到目前为止告诉我们的情况,这可能会起到作用:

select emp, date_of_joining
from your_table
where date_of_joining = (select max(date_of_joining)
                         from emp
                        );

答案 1 :(得分:5)

这将使最近的加入者只使用一次表扫描:

SELECT emp,
       date_of_joining
FROM   (
  SELECT emp,
         date_of_joining,
         MAX( date_of_joining ) OVER () AS max_date
  FROM   your_table
)
WHERE  date_of_joining = max_date;

这将获得最近的4个加入者:

SELECT *
FROM   (
  SELECT *
  FROM   your_table
  ORDER BY date_of_joining DESC
)
WHERE  ROWNUM <= 4;

这将获得最新的木匠(但在领带上返回倍数):

SELECT emp,
       date_of_joining
FROM   (
  SELECT emp,
         date_of_joining,
         RANK() OVER ( ORDER BY date_of_joining DESC ) AS rnk
  FROM   your_table
)
WHERE  rnk = 1;

答案 2 :(得分:3)

你可以用这个:

SELECT emp, date_of_joining
FROM table_name
WHERE date_of_joining = (
    SELECT MAX(date_of_joining)
    FROM table_name
    );

答案 3 :(得分:0)

SELECT * FROM
(
 SELECT *,Rank()OVER(order BY DOJ desc) RNO 
 FROM EMP
)A WHERE A.RNO =1

答案 4 :(得分:-1)

SELECT emp Date of joining
FROM TABLE_NAME
WHERE
Date_of_joining > '1/1/2011'

检查一下。根据你的日期添加日期。