SQL rank percentile

為{幸葍}努か 提交于 2019-12-02 13:59:00
SELECT  page, 
        views,
        (1-ranks/totals)*100 Percentile
FROM
(
    SELECT  page,
            views,
            @rank:=@rank + 1 ranks,
            (SELECT COUNT(*) FROM tableName) totals
    FROM    tableName a,
            (SELECT @rank:=0) s
    ORDER   BY views DESC
) s

You cannot calculate percentile ranks across a table in a single SQL statement. The approach suggested by John Woo here falls apart after the top ranks are calculated, even though the results do look good for the first (unpredictable) percent of the table being processed, meaning the top few percentiles.

The reason why is explained in this post by Oracle Ace Roland Bouman: http://rpbouman.blogspot.com/2009/09/mysql-another-ranking-trick.html

In short: user-defined variables are not designed to be reliable within a single SQL statement, only across multiple SQL statements.

Read the first sentence of the MySQL manual about User-Defined Variables: http://dev.mysql.com/doc/refman/5.5/en/user-variables.html "You can store a value in a user-defined variable in one statement and then refer to it later in another statement."

Then in about the 10th paragraph see this clear statement: "As a general rule, other than in SET statements, you should never assign a value to a user variable and read the value within the same statement. [. . .] the order of evaluation for expressions involving user variables is undefined. "

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!