I\'ve made an SQL query which rank pages by how many times they have been viewed. For instance,
╔══════╦═══════╗
║ PAGE ║ VIEWS ║
╠══════╬═══════╣
║ J ║ 10
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. "