How can you query to get the top 5 results by group in a group query?
I have a table which lists complaints by department. I need to be able to pull out the last 5 complaints for each department.
TIA
Peter
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Top 5 by Group in Query (A97)
See ACC: How to Create a Top Values Per Group Report
If I find how to do it in a query, I’ll post it.
This requires a subquery. Let’s suppose your tblComplaints has fields for Dept, ComplaintNo, and ComplaintDate. Your query would be something like this:
Select Dept, ComplaintDate, ComplaintNo from tblComplaints where ComplaintNo In (Select top 5 ComplaintNo from tblComplaints as C WHERE C.Dept=tblComplaints.Dept ORDER BY ComplaintDate DESC)
Just adding my with one more reference… although thanks to Mark I didn’t have to wade through them anymore
…
Some Database Journal query tutorials might come in handy for those who want to get some more theory (though I have too little experience to know whether this is the best on-line reference…). FYI, these are the links: Subqueries, part 1,Using the TOP keyword and Subqueries, part 2
Just adding my with one more reference… although thanks to Mark I didn’t have to wade through them anymore
…
Some Database Journal query tutorials might come in handy for those who want to get some more theory (though I have too little experience to know whether this is the best on-line reference…). FYI, these are the links: Subqueries, part 1,Using the TOP keyword and Subqueries, part 2
In ADO, you can use shaped recordsets to do this. See post 243358 but in A97 you’re probably stuck with subqueries.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications