[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Return entire row which contains max value

Posted on 2007-12-06
8
Medium Priority
?
1,682 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 93

Accepted Solution

by:
Patrick Matthews earned 400 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 93

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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 100 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 93

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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