Solved

How find top 3 records for each part number

Posted on 2014-03-31
3
396 Views
Last Modified: 2014-04-01
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.
0
Comment
Question by:SteveL13
3 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39966510
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?
0
 

Author Comment

by:SteveL13
ID: 39966541
Yes, largest RecID.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39966563
try this query


SELECT tblParts.Part_No, tblParts.RecID
FROM tblParts
WHERE (((tblParts.RecID) In (select top 3 RecID from tblParts as  P where P.Part_No=tblParts.Part_No Order By RecID Desc)))
ORDER BY tblParts.Part_No, tblParts.RecID DESC;
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question