Select a row by max(year) and group by name, but also include other values in the result set

43 Views Asked by At

i have a problem with a specific selection in a table. In would like to find all Persons grouped by Name with the max year. But I need also the other columns, for a later join, in the result set.

Furthermore, the id is unordered, so it can not be used in an aggregate function. The query should be database agnostic.

Here is the Table:

enter image description here

Here is the Result that I need:

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

You can try below- using correlated subquery

select * from tablename a
where year = (select max(year) from tablename b a.name=b.name)
1
On
select x.* from tbl x
join (select name, max(year) max_year from tbl group by name) y
on x.name = y.name and x.year = y.max_year