Solved

Return entire row which contains max value

Posted on 2007-12-06
8
1,654 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

786 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