Solved

Return entire row which contains max value

Posted on 2007-12-06
8
1,673 Views
Last Modified: 2010-04-21
In an MS Access Query, How do I return the entire row which contains the max() value of some field.
For instance, let's say I have a CustomerID field and an invoice_date field and the rest of the record, whatever it may be. There may be many records with this customerID, but I want to return the entire record that contains the newest (or oldest) invoice_date.
I usually do this with two queries, one to find the max(or min) date for the customer, then a second query to pull the entire record based on a two filed join on the customerID and invoice_date fields.
Is there a one-step way of doing this or is this the way that everyone does this?
0
Comment
Question by:LouieGee
[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
  • 4
  • 3
8 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 100 total points
ID: 20420776
SELECT s.*
FROM SomeTable s
WHERE s.SomeField =
    (SELECT Max(s2.SomeField) FROM SomeTable s2)
0
 

Author Comment

by:LouieGee
ID: 20421050
mathewspatrick,

You're doing a sub-select, which is what I figured, but it would have to be joined on both the customer ID   AND   the max value.
So in your example, how would that look?
Can a sub select be used to join on two fields?
Do you have to do two sub-selects?
Is there any SQL functionality to do this with one SQL command?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 20421160
Sorry, missed the customer part.

SELECT s.*
FROM SomeTable s INNER JOIN
    (SELECT s2.CustomerID, Max(s2.OtherField) AS MaxValue
    FROM SomeTable s2
    GROUP BY s2.CustomerID) AS z ON s.CustomerID = z.CustomerID AND s.[Value] = z.MaxValue
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 25 total points
ID: 20422547
Check if this works - haven't got Access on me, but worth trying

SELECT s.*
FROM SomeTable s
WHERE s.SomeField =
    (SELECT Max(s2.SomeField) FROM SomeTable s2 WHERE (s.CustomerID = s2.CustomerID))
0
 

Author Comment

by:LouieGee
ID: 20430913
The syntax in Access SQL is:
SELECT *
FROM invoices AS Inv1
WHERE (((Inv1.InvoiceDate)=Any (SELECT max([invoicedate]) FROM invoices WHERE [custnum]=Inv1.[custnum])));

Of course, Access likes to go a little crazy with parenthesis, but there it is.

So from what you guys are saying, there is no better way. Either you have to do two separate queries, which is how I normally do it   orrrrr   do a sub-select (which is, in effect, two queries).

Thanks, will figure out points soon unless someone else throws a hail-mary.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 20431095
LouieGee,

Yes, you either need a subquery, or two different queries.

BTW, did you try my second suggestion?  It ought to have worked...

Regards,

Patrick
0
 

Author Closing Comment

by:LouieGee
ID: 31413179
Matthew's first answer lacked only the second 'Where' clause, which was trivial to fix once I looked at the whole picture. Matthew's second answer works but Access seems to like to reformat it and then editing it causes syntax errors until you get it just right. Imitchie's answer was the same as Matthew's first one, but added the where clause for which Imitchie gets some of the points. (The missing where clause was trivial to fix)
My main question was: is there a better way, and the answer is "NO".
Actually, the answer is "Slightly"; now at least I can save it in one query and I won't have to remember that query 1 feeds into query 2.
0
 

Author Comment

by:LouieGee
ID: 20441230
Oh,
Sorry,

A big thanks to Patrick (not matthew) and Imitchie.

Louie G
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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.
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