Calculating percentile rankings in MS SQL

前端 未结 8 1630
清酒与你
清酒与你 2020-12-14 02:22

What\'s the best way to calculate percentile rankings (e.g. the 90th percentile or the median score) in MSSQL 2005?

I\'d like to be able to select the 25th, median,

8条回答
  •  既然无缘
    2020-12-14 02:43

    Percentile is calculated by

    (Rank -1) /(total_rows -1) when you sort values in ascending order.

    The below query will give you percentile value between 0 and 1. Person with lowest marks will have 0 percentile.

    SELECT Name, marks, (rank_1-1)/((select count(*) as total_1 from table)-1)as percentile_rank
    from
    (
    SELECT Name,
           Marks,
           RANK() OVER (ORDER BY Marks) AS rank_1
           from table
    ) as A
    

提交回复
热议问题