I am using PostgreSQL 9.4.
I have workout lists
and an associated join table enumerating which workouts are in that list.
I'd like to return the 5 most recent results for each workout in the list.
The below returns every result, and if I append LIMIT 5
, I only get a total of 5 results, rather than 5 per workout. How do I do this in SQL?
SELECT "results".* FROM "results"
WHERE "results"."user_id" = 1
AND workout_id IN (SELECT workout_id FROM workout_list_join_table
WHERE workout_list_id = 5)
ORDER BY "results"."done_at" DESC