MySQL Group By order

This is a discussion on MySQL Group By order within the MySQL Database forums, part of the Database Forums category; I'm trying to GROUP BY a column and have the row with the latest timestamp to be returned. Example: ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-26-2008
ryanmhuc@yahoo.com
 
Posts: n/a
Default MySQL Group By order

I'm trying to GROUP BY a column and have the row with the latest
timestamp to be returned. Example:

TABLE Test

id | text | timestamp
1 , 'A' , 2008-03-01
1 , 'B' , 2008-03-26

SELECT *
FROM Test
GROUP BY id

RESULTS:
1 , 'A' , 2008-03-01

I need the results to be the row with the latest timestamp:
1 , 'B' , 2008-03-26

Of course a nexted query can produce this result but this will create
tempory table which will be inefficiant. Is there a way to do with
without a nested query. By perhaps somehow ordering before the group
by?

Reply With Quote
  #2 (permalink)  
Old 03-26-2008
strawberry
 
Posts: n/a
Default Re: MySQL Group By order

On Mar 26, 7:52 pm, "ryanm...@yahoo.com" <ryanm...@yahoo.com> wrote:
> I'm trying to GROUP BY a column and have the row with the latest
> timestamp to be returned. Example:
>
> TABLE Test
>
> id | text | timestamp
> 1 , 'A' , 2008-03-01
> 1 , 'B' , 2008-03-26
>
> SELECT *
> FROM Test
> GROUP BY id
>
> RESULTS:
> 1 , 'A' , 2008-03-01
>
> I need the results to be the row with the latest timestamp:
> 1 , 'B' , 2008-03-26
>
> Of course a nexted query can produce this result but this will create
> tempory table which will be inefficiant. Is there a way to do with
> without a nested query. By perhaps somehow ordering before the group
> by?


Look for "strawberry query" in this NG.
Reply With Quote
  #3 (permalink)  
Old 03-27-2008
ryanmhuc@yahoo.com
 
Posts: n/a
Default Re: MySQL Group By order

On Mar 26, 5:47*pm, strawberry <zac.ca...@gmail.com> wrote:
> On Mar 26, 7:52 pm, "ryanm...@yahoo.com" <ryanm...@yahoo.com> wrote:
>
>
>
>
>
> > I'm trying to GROUP BY a column and have the row with the latest
> > timestamp to be returned. Example:

>
> > TABLE Test

>
> > id | text | timestamp
> > 1 , 'A' , 2008-03-01
> > 1 , 'B' , 2008-03-26

>
> > SELECT *
> > FROM Test
> > GROUP BY id

>
> > RESULTS:
> > 1 , 'A' , 2008-03-01

>
> > I need the results to be the row with the latest timestamp:
> > 1 , 'B' , 2008-03-26

>
> > Of course a nexted query can produce this result but this will create
> > tempory table which will be inefficiant. Is there a way to do with
> > without a nested query. By perhaps somehow ordering before the group
> > by?

>
> Look for "strawberry query" in this NG.- Hide quoted text -
>
> - Show quoted text -


Thanks strawberry you're the man. For anyone else that may be stuck
searching besides know as the Strawberry Query thanks to Strawberry
shedding some light on it there are references to Group-wise
Maximum.

http://dev.mysql.com/doc/refman/5.0/...group-row.html
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0