I have a score model where users can play and add scores. When I pick the highscore I don't want to let users have more than one score on the scoreboard.
Therefore I need to sort the score by points and group/distinct on user_id. I have gotten all sorts of errors,
...must appear in the GROUP BY clause or be used in an aggregate function...
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
This is my query:
Score.all(:select => "DISTINCT ON (user_id) *", :order_by => "points DESC")
distinct on (user_id, points)
– Denis Jun 1 '13 at 12:12