TOP 1 Query from each ID with multiple instances

折月煮酒 提交于 2019-12-23 12:51:57

问题


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

回答1:


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

SELECT ID, MAX(DATE)
FROM [table]
GROUP BY ID


来源:https://stackoverflow.com/questions/9038980/top-1-query-from-each-id-with-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!