I have a MySQL SELECT query which uses 20 different comparisons within the same table. Here’s an example:
SELECT * FROM mytable
WHERE (col1 > (col2 * 0.25))
AND (col5 < col10) .......
I’m trying to calculate percentile ranks based on the order of a column called SCORE within the SELECT results returned. I’ve tried using incremental row numbers and COUNT(*) to get the stock’s rank and total number of results returned but not sure how to assign the same rank where some of the results have the same SCORE.
Here’s the formula that I’m trying to calculate:
((COUNT(lower scores) + (COUNT(same/tied scores) / 2)) * 100) / COUNT(total results)
How do I find the number of lower scores, same/tied scores and total scores within the same result row for calculating percentiles on the fly?
I’m trying to avoid using stored procedures because I want to my application’s admins to tailor the SELECT statement within my applications admin area as needed.
Using Shlomi’s code above, here’s the code that I came up with to calculate percentile ranks (in case anyone wants to calculate these in the future):