我一直在玩这个 SQL 代码:
SELECT
id,
@prev := @curr as prev,
@curr := measure as curr,
@rank := IF(@prev > @curr, @rank+@ties, @rank) AS rank,
@ties := IF(@prev = @curr, @ties+1, 1) AS ties,
(1-@rank/@total) as percentrank
FROM
mytable,
(SELECT
@curr := null,
@prev := null,
@rank := 0,
@ties := 1,
@total := count(*) from mytable where measure is not null
) b
WHERE
measure is not null
ORDER BY
measure DESC
我想在名为“percentile”的列中将计算出的“percentrank”写回 mytable 的每个相应行,但我不记得如何在我的更新语句中工作。
我很感激帮助。
归功于SQL 的http://code.openark.org/blog/mysql/sql-ranking-without-self-join。