I\'ve been surprisingly unable to find an nth percentile function for postgresql.
I am using this via mondrian olap tool so i just need an aggregate function which r
The ntile
function is very useful here. I have a table test_temp
:
select * from test_temp
score
integer
3
5
2
10
4
8
7
12
select score, ntile(4) over (order by score) as quartile from test_temp;
score quartile
integer integer
2 1
3 1
4 2
5 2
7 3
8 3
10 4
12 4
ntile(4) over (order by score)
orders the columns by score, splits it into four even groups (if the number divides evenly) and assigns the group number based on the order.
Since I have 8 numbers here, they represent the 0th, 12.5th, 25th, 37.5th, 50th, 62.5th, 75th and 87.5th percentiles. So if I only take the results where the quartile
is 2, I'll have the 25th and 37.5th percentiles.
with ranked_test as (
select score, ntile(4) over (order by score) as quartile from temp_test
)
select min(score) from ranked_test
where quartile = 2
group by quartile;
returns 4
, the third highest number on the list of 8.
If you had a larger table and used ntile(100)
the column you filter on would be the percentile, and you could use the same query as above.