Solved

Return entire row which contains max value

Posted on 2007-12-06
8
1,635 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

708 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

15 Experts available now in Live!

Get 1:1 Help Now