Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

How find top 3 records for each part number

I have a table named tblOpenOrders.  The primary key field is named RecID.  Also in the table is a field named Part_No.  I am trying to write a query using query designer that finds the top three records in the table, with RecID being a number field FOR EACH part in Part_No.

So the resulting query might look like:

Part_No       RecID
12345          98234
12345          97620
12345          97100
45612          99124
45612          99005
45612          98307
78451          97890
78451          96120  (sometimes there could be only 2 or 3 records for a given Part_No)
etc.
etc.
etc.
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

The LIMIT function in SQL limits the total number of records returned by the query, it can't be used to limit the number of records that match a sub query.

You would need to do it with an initial query that gets you the PartNo's and then loop through the part numbers to get the top 3 records for each part number.

What do you mean by top 3 by the way? largest Recnum, or something else?
Avatar of SteveL13

ASKER

Yes, largest RecID.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial