Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TOP 1 Query from each ID with multiple instances

This query will return the top for all rows in MS Access.

SELECT TOP 1 * FROM [table]
ORDER BY table.[Date] DESC;

I need to return the top date for each id that can have multiple dates.

ID      DATE
1      01/01/2001
1      01/12/2011
3      01/01/2001
3      01/12/2011

Should return only the top dates like this.

1      01/12/2011
3      01/12/2011
like image 440
Allan Jason Avatar asked Jan 17 '23 15:01

Allan Jason


1 Answers

You'll want to use the MAX function, along with a GROUP BY.

SELECT ID, MAX(DATE)
FROM [table]
GROUP BY ID
like image 168
Michael Fredrickson Avatar answered Jan 22 '23 06:01

Michael Fredrickson