Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top 5 by Group query

Status
Not open for further replies.

860Sharp

Technical User
Aug 10, 2013
4
US
I have a large file of athletic data. I have already made a query called "Rank by Pace by School" That has Sorted the data returning the fastest times per kid (no repeated kids) Per school. Table is called "Main Data"

File looks like (expr1 is the mile pace in secs)
School Name expr1
a Joe 357.7
a Jim 367.7
a John 378.7
a Fred 391.1
a Alan 395.7
a Frank 397.5
a Harry 398.7
a Bob 399.7
a Alan 400.2
b Alex 357.7
b Bill 367.7
....
I would like a query that takes the query above and only returns the top 5 results per school.
So for school a, it would only return results Joe through Alan. For school b returning only Alex through...whoever.

Can someone help me with this. I am trying to do it through design mode, but that may not be possible without going into SQL
 
Like this ?
SQL:
SELECT A.School, A.Name, A.expr1
FROM [Rank by Pace by School] AS A INNER JOIN [Rank by Pace by School] AS B ON A.School=B.School AND A.expr1<=B.expr1
GROUP BY A.School, A.Name, A.expr1
HAVING Count(*)<=5
ORDER BY 1,3 DESC

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Well it seems to be working. Only it is giving me the 5 slowest, not the 5 fastest
 
Sorry for the typo.
Replace this:
A.expr1<=B.expr1
with this:
A.expr1>=B.expr1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Follow up question. Is there a way to make it show only the teams that have 5 runners to show. Eliminating teams with only 1-2 athletes?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top