# Order By Before Group By

**URL:** <https://forums.percona.com/t/order-by-before-group-by/689>\
**Category:** Other MySQL® Questions\
**Created:** [March 26, 2008, 1:42pm UTC](https://forums.percona.com/t/order-by-before-group-by/689 "2008-03-26T13:42:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jadent](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jadent](https://forums.percona.com/u/jadent)\
**Post date:** [March 26, 2008, 1:42pm UTC](https://forums.percona.com/t/order-by-before-group-by/689/1 "2008-03-26T13:42:36Z")

</div>

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

I know i can nest queries to do this but this will result in temporary tables which will not be optimized. Is it possible to Order By before the Group or get this result using another method?

Thanks

---

<div class="post-metadata">

**Author:** ![sterin](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@sterin](https://forums.percona.com/u/sterin)\
**Post date:** [March 27, 2008, 4:17am UTC](https://forums.percona.com/t/order-by-before-group-by/689/2 "2008-03-27T04:17:01Z")

</div>

This is what you want:

SELECT id, MAX(timestamp)FROM TestGROUP BY id

Read about the group by functions here:  
[http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.ht](http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.ht) ml

---

<div class="post-metadata">

**Author:** ![jadent](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jadent](https://forums.percona.com/u/jadent)\
**Post date:** [March 27, 2008, 7:48am UTC](https://forums.percona.com/t/order-by-before-group-by/689/3 "2008-03-27T07:48:34Z")

</div>

Thanks for the reply but that will not work as it will not return the row with the greatest timestamp only the greatest timestamp for all the rows.

I did find the answer i was seeking at MySQL. This type of query is referred to as a Group-wise Maximum query.

[http://dev.mysql.com/doc/refman/5.0/en/example-maximum-colum](http://dev.mysql.com/doc/refman/5.0/en/example-maximum-colum) n-group-row.html

In Google Groups some have labelled it a Strawberry Query.

---

<div class="post-metadata">

**Author:** ![msureshkumarbest](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@msureshkumarbest](https://forums.percona.com/u/msureshkumarbest)\
**Post date:** [January 29, 2009, 6:01am UTC](https://forums.percona.com/t/order-by-before-group-by/689/4 "2009-01-29T06:01:09Z")

</div>

hi jadent send that code

---

<div class="post-metadata">

**Author:** ![alkot](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@alkot](https://forums.percona.com/u/alkot)\
**Post date:** [July 22, 2009, 6:27am UTC](https://forums.percona.com/t/order-by-before-group-by/689/5 "2009-07-22T06:27:57Z")

</div>

I know it’s a bit late, but still useful for newcomers.

If there’s a PK or a UNIQUE constraint (on 1 or many fields), that may work :

SELECT Test.\*FROM TestINNER JOIN ( SELECT MAX(id) id FROM Test GROUP BY gid) s ON s.id = Test.id

… where id is PK of table ‘Test’ and gid, the field to group by.
