可能重复:
How to display the record with the highest value in Oracle?
我应该从数据库中显示最老的人的姓名和年龄。我使用的代码显示了几个人的姓名和计算年龄,但它不会显示单个最老的人。我如何让它工作? 这是我的代码
select dr_drvname as "Name", trunc(max((sysdate-dr_brthdate)/365)) as "Highest AGE"
from driver,dual
group by dr_drvname;
答案 0 :(得分:2)
Oracle的另一种方式是:
SELECT
dr_drvname AS "Name",
trunc(months_between(sysdate, dr_birthdate) / 12) AS "Highest Age"
FROM
(
SELECT
dr_drvname,
dr_birthdate
FROM
driver
ORDER BY
dr_birthdate
)
WHERE
ROWNUM = 1;
答案 1 :(得分:1)
尝试这样的事情:
SELECT top 1
Firstname, LastName, Max([BirthDate]) as Oldest
FROM [Employees]
group by BirthDate, FirstName, LastName
注意:如果多人有相同的出生日期,有哪些要求?
答案 2 :(得分:1)
您可以使用分析函数
select dr_drvname as "Name",
trunc( months_between( sysdate, dr_birthdate )/ 12 ) "Highest Age"
from (select dr_drvname ,
dr_birthdate,
rank() over (order by dr_birthdate) rnk
from driver)
where rnk = 1;