Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

filter for top 10

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
c - 11 Nov 2005 16:27 GMT
I have a query with the following 3 fields:

Customer     Item #    Qty Ordered

There are 5 customers, and up to 100 items.  I want to filter the top 10 for
each customer.  Currently the query is sorted by customer name, then
descending by Qty Ordered.  Looks like:

Customer    Item #   Qty Ordered
Bob             Item A       1000
Bob             Item B         700
Bob             Item C         500

If I group by Max under Qty Ordered I only get the highest, I need the 10
highest.  Is there any way to do this?

Thanks,
c
John Spencer - 11 Nov 2005 17:15 GMT
Yes there is a way.  You need to use a correlated subquery.

SELECT Customer, [Item #], [qty Ordered]
FROM SomeQueryOrTable
WHERE [qty Ordered] in
(SELECT TOP 10 tmp.[Qty Ordered]
 FROM SomeQueryOrTable as Tmp
 WHERE tmp.Customer = SomeQueryOrTable.Customer
 ORDER BY tmp.[Qty Ordered] DESC, Tmp.[Item #])

That should give you 10 items and quantities for each customer in the
query/table.  If you want ties, then drop the Tmp.[Item #] from the ORDER BY
clause in the Sub-query.

>I have a query with the following 3 fields:
>
[quoted text clipped - 15 lines]
> Thanks,
> c
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.