简体   繁体   中英

Access SQL Query Top 1 of Group

I am trying to write a query that selects the Top record in each Group of Data.

eg Below

Division Team Points
1 Liverpool 90
1 Manchester 88
2 Leeds 94
2 Arsenal 77
3 Bolton 66
3 Spurs 72
3 Derby 33

I want my query to return the team with highest number of points in each division:

Division Team Points
1 Liverpool 90
2 Leeds 94
3 Spurs 72

I thought this should be easy.

Any ideas?

Thanks

In MS Access, you can use:

select t.*
from t
where t.points = (select max(t2.points)
                  from t t2
                  where t2.division = t.division
                 );

If there are ties, then this returns all matching teams. If you want only one team even when there are ties:

select t.*
from t
where t.team = (select t2.team
                from t t2
                where t2.division = t.division
                order by t2.points desc, t2.team
               );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM