Sunday, November 1, 2009

MySQL: Rank calculation

SELECT ID,
@rank := COALESCE(
NULL + (@prev := @curr),
NULL + (@curr := r.score),
NULL + (@counter := @counter+1),
IF(@prev = @curr, @rank, @counter)
) AS Rank

FROM
(SELECT @curr:=null,@prev:=null,@rank:=0,@counter:=0) sel1,
Record AS r

ORDER BY r.score DESC

No comments:

Post a Comment