MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 31st, 2008, 11:04 PM
mperemsky mperemsky is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 mperemsky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 14 sec
Reputation Power: 0
How To: get records using max date

I have never had to do this before, I have looked but have not been able to find a solution.

I have a table with the following fields:

ID | UpdateDt | Val
1 2008-01-01 1
1 2008-01-02 4
1 2008-01-10 7
2 2008-01-01 3
2 2008-01-05 5
3 2008-01-01 7
3 2008-01-06 4

I want to retrieve the row for each id with the max date

1 2008-01-10 7
2 2008-01-05 5
3 2008-01-06 4

I have tried various ways, the closest I got was to get the max date, but the first row for each id.

Reply With Quote
  #2  
Old June 2nd, 2008, 11:06 AM
dykebert's Avatar
dykebert dykebert is offline
Contributing User
Click here for more information. Click here for more information
 
Join Date: Apr 2008
Posts: 348 dykebert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 10 h 53 m 31 sec
Reputation Power: 1
OK well getting the val field makes this a bit tricky. I haven't entered your data to make sure this will work, but it should.

Code:
SELECT <tbl>.ID, <tbl>.UpdateDt, <tbl>.Val
FROM <tbl> INNER JOIN (SELECT <tbl>.ID, Max(<tbl>.UpdateDt) as MaxDate FROM <tbl> GROUP BY ID) as TMax
ON <tbl>.ID = TMax.ID AND <tbl>.UpdateDt = TMax.MaxDate
ORDER BY <tbl>.ID


Enjoy!

Reply With Quote
  #3  
Old June 2nd, 2008, 06:57 PM
mperemsky mperemsky is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 mperemsky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 14 sec
Reputation Power: 0
It worked!

Thank you very much that worked great!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > How To: get records using max date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT