Solved

How find top 3 records for each part number

Posted on 2014-03-31
3
390 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 119

Accepted Solution

by:
Rey Obrero 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now