Solved

Return entire row which contains max value

Posted on 2007-12-06
8
1,668 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

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