Calculating percentile rankings in MS SQL

前端 未结 8 1641
清酒与你
清酒与你 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:40

    i'd do something like:

    select @n = count(*) from tbl1
    select @median = @n / 2
    select @p75 = @n * 3 / 4
    select @p90 = @n * 9 / 10
    
    select top 1 score from (select top @median score from tbl1 order by score asc) order by score desc
    

    is this right?

提交回复
热议问题