Aggregate keeping the row with the max value
Suppose 开发者_如何学Cyou have the following schema (id, user_id , score). How can I take per each user the row with max score and then order all row for score. In other word I want a ranking where each user have his best result.
select user_id, max(score)
from user_scores
group by user_id
order by max(score)
Should be something like:
SELECT UNIQUE user_id, score FROM TABLE
ORDER BY SCORE DESC
select @rownum := @rownum + 1 AS rank, user_id, MAX(score) as Score
from table_name t,
(SELECT @rownum := 0) r
GROUP BY user_id
ORDER BY Score
精彩评论