Sorry for the bad title.
If have an SQLite database with a table with workout Sets like this:
id: INT,
exerciseId: Int,
weight: Double,
repCount: Int,
completedAt: Date
Now I want to get the best Set of every exercise. I can do it for a single exercise (with id=123) like so:
SELECT *
FROM Set
WHERE exerciseId = 123
ORDER BY weight DESC, repCount DESC, completedAt ASC
LIMIT 1
My question is, how would a query look like, that would give me the best set for all exercises? Is that even possible? I tried with GROUP BY etc. but since 'best' is derived from multiple columns that does not work correctly.
Window function would be the best as shared in the above answers, I wanted to share another approach, I have compared the results with window functions and it shows the same result set
SELECT s.*
FROM Set1 s
WHERE s.id = (
SELECT id
FROM Set1
WHERE exercise_id = s.exercise_id
ORDER BY weight DESC, rep_count DESC, completed_at ASC
LIMIT 1
);