General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsDatabasesGeneral SQL 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 October 1st, 2004, 04:22 PM
MiP MiP is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 MiP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to MiP
Question select MAX(DATETIME) problem

I have this simple table:
ID------DATE-------GRP
1----2004-08-02----1
2----2004-08-03----1
3----2004-10-17----3
4----2004-08-02----2
5----2003-02-12----2
6----2004-02-03----2
7----2004-01-20----1
8----2004-07-04----3

I want select last DATE in each GRP
When I execute this SQL query:
SELECT *,max(DATE) as max_date FROM table group by GRP order by max_date desc;

I'll receive result:
id, date, grp
8,2004-10-17,3
7,2004-08-03,1
5,2004-08-02,2

DATE and GRP are right but ID isn't correct. What's wrong??? I have no idea

Reply With Quote
  #2  
Old October 16th, 2004, 04:06 AM
michlmann michlmann is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 56 michlmann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
You can't group by GRP and also select ID. This doesn't make sense, because (to the DB) you might have two IDs with the same maximum DATE and GRP.

First, you may want to SELECT the maximum DATE per GRP.

Code:
SELECT grp, max(date) as max_date FROM table GROUP BY grp


Now you may use this query to join it with another query containing the IDs:

Code:
SELECT a.id, a.grp, a.date 
      FROM table a, (SELECT grp, max(date) AS max_date FROM table GROUP BY grp) b
      WHERE a.grp = b.grp AND a.date = b.max_date

Reply With Quote
  #3  
Old January 21st, 2009, 02:14 AM
jabrik jabrik is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 1 jabrik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 31 sec
Reputation Power: 0
SELECT MAX(ID) as maxid, GRP, MAX(DATE) AS max_date
FROM table
GROUP BY GRP

I haven't check the code, but perhaps it will help you

Reply With Quote
  #4  
Old June 8th, 2010, 04:59 AM
kplow1986 kplow1986 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 1 kplow1986 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 55 sec
Reputation Power: 0
SELECT id,date,grp FROM table group WHERE DATE=
(SELECT max(date) FROM table group) GROUP BY GRP

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > select MAX(DATETIME) problem


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap