sqlansi-sql-92

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


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


Solution

  • You can try below- using correlated subquery

    select * from tablename a
    where year = (select max(year) from tablename b a.name=b.name)