Solved

How find top 3 records for each part number

Posted on 2014-03-31
3
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

726 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