Seemingly similar to How to get the top 10 values in postgresql?, yet somehow very different.
We’ll set it up similar to that question:
I have a postgresql table: Scores(score integer)
.
How would I get the highest 99% of the scores? We cannot say that we know beforehand how many rows there are, so we can’t use the same limit to an integer trick. SQL Server has an easy SELECT TOP
syntax — is there anything similarly simple in the postgresql world?
2
Answers
This should be doable with
percent_rank()
you can use the ntile function to partition the rows into percentiles and then select the rows where tile > 99
example: