Skip to content

Instantly share code, notes, and snippets.

@skarger
Last active June 14, 2019 19:01
Show Gist options
  • Save skarger/5c8508cf2ebaba915cea573be6af85ef to your computer and use it in GitHub Desktop.
Save skarger/5c8508cf2ebaba915cea573be6af85ef to your computer and use it in GitHub Desktop.
SELECT workouts.*
FROM workouts JOIN (
SELECT max(id) AS id FROM (
SELECT w.id, w.sport_id
FROM workouts w JOIN (
SELECT sport_id, max(created_at) AS created_at
FROM workouts
GROUP BY sport_id
) latest_by_sport
ON w.created_at = w.created_at
AND w.sport_id = latest_by_sport.sport_id
) all_most_recent_by_sport
GROUP BY sport_id
) one_most_recent_by_sport
ON one_most_recent_by_sport.id = workouts.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment