0

I have a table which contains records

id  note    date    timestamp
1   ABCD    2014-08-18  2014-08-18 08:33:49
2   ABCDE   2014-08-18  2014-08-18 08:33:49
3   ABCDEF  2014-08-19  2014-08-18 08:33:49
4   XYZ     2014-08-19  2014-08-19 15:25:15
5   ABCD    2014-08-20  2014-08-19 15:25:15
6   ABCD    2014-08-19  2014-08-20 19:30:02

I need to group on "note" & select values created by last timestamp.

My expected output should be:

id  note    date    timestamp
2   ABCDE   2014-08-18  2014-08-18 08:33:49
3   ABCDEF  2014-08-19  2014-08-18 08:33:49
4   XYZ     2014-08-19  2014-08-19 15:25:15
6   ABCD    2014-08-19  2014-08-20 19:30:02

I am using query

SELECT Field1, Field2, Max(TimeStamp) maxTime FROM table GROUP BY Field1

from post "MySQL Select Group of Records Based on latest timestamp"

But the query selects last time stamp, but the date field will be of previous record..

Please check the SQL fiddle :http://sqlfiddle.com/#!2/92a0ab/1 and let me know where i am wrong, or can somebody help me with correct query!!!

Community
  • 1
  • 1
user1796743
  • 39
  • 1
  • 10
  • possible dup http://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column – James Aug 21 '14 at 08:16

1 Answers1

4

You can try

SELECT * FROM (SELECT * FROM table ORDER BY timestamp DESC) AS a GROUP BY a.note ORDER BY a.timestamp

I think it help for you :)

Habibul Morsalin
  • 180
  • 1
  • 11