Skip to content

Instantly share code, notes, and snippets.

@Cezarion
Created December 11, 2013 09:07
Show Gist options
  • Select an option

  • Save Cezarion/7907226 to your computer and use it in GitHub Desktop.

Select an option

Save Cezarion/7907226 to your computer and use it in GitHub Desktop.
Select all rows by max date
/* --------------------------------------------------------------
DB MODEL
ID Type_id Name Value Date
-- -------- ------ ------- ---------------------
1 56 "name1" "lorem" "2011-03-19 22:08:13"
5 58 "name2" "ipsum" "2011-03-19 22:00:44"
22 58 "name2" "dolor" "2011-03-20 11:03:13"
31 25 "name3" "sit" "2011-04-28 18:42:07"
44 12 "name4" "amet" "2011-04-12 11:06:11"
98 45 "name5" "consectetur" "2011-03-19 22:46:04"
211 12 "name4" "adipiscing" "2011-03-19 22:25:24"
217 25 "name3" "elit" "2011-03-19 22:25:24"
-------------------------------------------------------------- */
SELECT * FROM my_table
INNER JOIN (SELECT Type_id, MAX(Date) as TopDate FROM my_table GROUP BY Type_id ) AS EachItem
ON EachItem.TopDate = my_table.Date
AND EachItem.Type_id = my_table.Type_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment