Solved

Access Query to filter table by Highest Value found in two Fields + iif logic

Posted on 2008-10-08
3
630 Views
Last Modified: 2013-11-28
I have a table in Access with the following fields (see attachment 1):
ParentPart
Version
Route
Component
QtyPer

I would like to write a query using the logic of the following psuedocode:
If Version is null, then return each record for each "ParentPart" where the "Route" is the highest value for that "ParentPart", If Version is not null, then ignore the route and return each record for each "ParentPart" where the "Version" is the highest value for that "ParentPart".

Basically for each parent part I want to grab the entire row if the version number is the highest value in the entire table or if version is null if the route is the highest value.

The second attached file shows what the Output of the query would be for the query.

Thanks for the help







table.jpg
table2.jpg
0
Comment
Question by:Cubemaster
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22676003
Try with a derived table:
SELECT b1.*

FROM BOMTable b1 

INNER JOIN (SELECT ParentPart, Max(Version) AS maxVersion, Max(Route) AS maxRoute FROM BOMTable GROUP BY ParentPart) b2

ON b1.ParentPart = b2.ParentPart

AND (b1.Version = b2.maxVersion 

     OR (IsNull(b1.Version)=1 AND b1.Route = b2.maxRoute))

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

914 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

12 Experts available now in Live!

Get 1:1 Help Now