'Get oldest date in database SQL [duplicate]
select
max((date '2019-07-01' - applicant.applicantbirthdate) / 365 as age,
applicant.applicantfirstname,
applicant.applicantlastname,
office.officename
from
applicant
inner join
office on office.officeid = applicant.officeid
group by
applicant.applicantfirstname, applicant.applicantlastname,
office.officename;
I'm not really sure what to do here. I'm suppose to pick the oldest user and list their office. but this shows the oldest user for all offices. how can I just only show the oldest person out of all the offices
Solution 1:[1]
This example is based on Scott's sample schema whose tables simulate what you have.
SQL> SELECT ename, hiredate
2 FROM emp
3 ORDER BY hiredate DESC;
ENAME HIREDATE
---------- ----------
ADAMS 12.01.1983 --> Adams has the MAX hiredate; you want his data
SCOTT 09.12.1982
MILLER 23.01.1982
FORD 03.12.1981
JAMES 03.12.1981
KING 17.11.1981
MARTIN 28.09.1981
TURNER 08.09.1981
CLARK 09.06.1981
BLAKE 01.05.1981
JONES 02.04.1981
WARD 22.02.1981
ALLEN 20.02.1981
SMITH 17.12.1980
14 rows selected.
Using a CTE which includes the RANK analytic function that "sorts" rows by hiredate in descending order (so that people - who have the MAX hiredate - rank as "highest")), in final query you just fetch those whose rank = 1:
SQL> WITH
2 temp
3 AS
4 (SELECT e.ename,
5 d.dname,
6 e.hiredate,
7 RANK () OVER (ORDER BY e.hiredate DESC) rn
8 FROM emp e JOIN dept d ON e.deptno = d.deptno)
9 SELECT ename, dname, ROUND ((SYSDATE - hiredate) / 365) age
10 FROM temp
11 WHERE rn = 1;
ENAME DNAME AGE
---------- -------------- ----------
ADAMS RESEARCH 39
SQL>
Applied to your query:
WITH
temp
AS
(SELECT a.applicantfirstname,
a.applicantlastname,
o.officename,
a.birthdate,
RANK () OVER (ORDER BY a.birthdate DESC) rn
FROM applicant a JOIN office o ON o.officeid = a.officeid)
SELECT applicantfirstname,
applicantlastname,
officename,
ROUND ((SYSDATE - birthdate) / 365) AS age
FROM temp
WHERE rn = 1;
Solution 2:[2]
#The below code should work. I recommend to go with RANK over if its large dataset but if its small dataset the below code works fine as well.
select applicant.applicantfirstname, applicant.applicantlastname, office.officename, max((date '2019-07-01' - applicant.applicantbirthdate) / 365 as age
FROM
applicant
INNER JOIN office ON office.officeid = applicant.officeid
WHERE
applicant.applicantbirthdate IN (
SELECT MIN(applicant.applicantbirthdate) FROM applicant);
Solution 3:[3]
Try the below one. There are two things missing in your query.
- We should list them based on descending order. So putting
ORDER BY age DESCwould ensure it. - We need to get the oldest candidate in terms of age. So
LIMIT 1would ensure that we would get only one record.
select (date '2019-07-01' - applicant.applicantbirthdate)/365 as age, applicant.applicantfirstname, applicant.applicantlastname, office.officename
from applicant
inner join office on office.officeid = applicant.officeid
group by applicant.applicantfirstname, applicant.applicantlastname, office.officename
order by age desc
limit 1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Littlefoot |
| Solution 2 | Developer |
| Solution 3 |
