One way to think about it is "the percentage of students with a score lower than this."
Here is one way to get this type of percentile in SQL Server using RANK()
:
select * , (rank() over (order by Score) - 1.0) / (select count(*) from @temp) * 100 as PercentileRank from @temp
Note that this will always be less than 100% unless you round and you always get 0% for the smallest value. This does not necessarily mean that the median value is 50%, and it will not interpolate, as some percentile calculations do.
Remember to round or distinguish the whole expression (for example, cast(... as decimal(4,2))
) for good reports, or even replace - 1.0
with - 1e
to force a floating point calculation.
NTILE()
is actually not what you are looking for in this case, because it essentially divides the number of rows of an ordered set into groups, not values. It will assign a different percentile to two instances of the same value if these instances intersect with the intersection point. Then you have to further group this value and capture the maximum or minimum percentile of the group in order to use NTILE()
in the same way as we do with RANK()
.
source share