Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How find top 3 records for each part number

Posted on 2014-03-31
3
Medium Priority
?
407 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

604 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