Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: find percentiles

Tags:

I have a table that has a certain numeric column called Score. I would like to execute a query on that table, whose result will have 100 rows, each representing the score corresponding with that percentile. For example, a result may look as follows:

 Percentile | Score
---------------------
 01         | 10
 02         | 12
 03         | 12
 04         | 17
 ...        | ...
 99         | 1684
 100        | 1685

The Score values in the result table above are actual score values that exist in the original table, and are not interpolated. An interpolated result will be better but is not a requirement.

There may be several heuristics that can produce such a result. What I'm using today (in code) is basically as follows - The Score value corresponding with a percentile will be the score value for which: the number of rows with smaller scores, divided by the total number of rows, rounded to an integer, equals to the percentile (I hope that's clear)

I can consider other heuristics if they are easier to implement

I'm working in MS-SQL, but would appreciate a solution that also works on MySQL.

What's the best way to achieve this?

like image 596
Roee Adler Avatar asked Sep 11 '09 10:09

Roee Adler


1 Answers

In SQL Server:

SELECT  percentile, score
FROM    (
        SELECT  ROW_NUMBER() OVER (PARTITION BY percentile ORDER BY score) AS rn, percentile, score
        FROM    (
                SELECT  score, NTILE(100) OVER (ORDER BY score) AS percentile
                FROM    mytable
                ) q
        ) q2
WHERE   rn = 1
like image 115
Quassnoi Avatar answered Oct 13 '22 09:10

Quassnoi