troubleshooting Question

In MySQL, how do I select a set of records from the most recent date?

Avatar of SiobhanElara
SiobhanElaraFlag for United States of America asked on
MySQL Server
5 Comments2 Solutions93 ViewsLast Modified:
I'm trying to select the data from the most recent date/year in a table. For example, I have the following data:

countyID   | datadate   | dataPercentage
1             | 2015          | 54
2             | 2015          | 60
3             | 2015          | 51
1             | 2016          | 54
2             | 2016          | 59
3             | 2016          | 53
1             | 2017          | 56
2             | 2017          | 64
3             | 2017          | 59
1             | 2018          | 56
2             | 2018          | 72
3             | 2018          | 57

I want to get the result:
1             | 2018          | 56
2             | 2018          | 72
3             | 2018          | 57

From what I've read, I got the impression the following query would work:
SELECT countyID, dataPercentage, MAX(dataDate) 
FROM myTable
GROUP BY countyID

But that returns the first set of percentages (from 2015) with the most recent date.
1             | 2018          | 54
2             | 2018          | 60
3             | 2018          | 51

What am I doing wrong and what should I be doing instead? Thanks!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros