Calculating percentile rankings in MS SQL

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

    I would think that this would be the simplest solution:

    SELECT TOP N PERCENT FROM TheTable ORDER BY TheScore DESC
    

    Where N = (100 - desired percentile). So if you wanted all rows in the 90th percentile, you'd select the top 10%.

    I'm not sure what you mean by "preferably in a single record". Do you mean calculate which percentile a given score for a single record would fall into? e.g. do you want to be able to make statements like "your score is 83, which puts you in the 91st percentile." ?

    EDIT: OK, I thought some more about your question and came up with this interpretation. Are you asking how to calculate the cutoff score for a particular percentile? e.g. something like this: to be in the 90th percentile you must have a score greater than 78.

    If so, this query works. I dislike sub-queries though, so depending on what it was for, I'd probably try to find a more elegant solution. It does, however, return a single record with a single score.

    -- Find the minimum score for all scores in the 90th percentile
    SELECT Min(subq.TheScore) FROM
    (SELECT TOP 10 PERCENT TheScore FROM TheTable
    ORDER BY TheScore DESC) AS subq
    
    0 讨论(0)
  • 2020-12-14 02:35

    i'd probably use a the sql server 2005

    row_number() over (order by score ) / (select count(*) from scores)

    or something along those lines.

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-12-14 02:43

    Check out the NTILE command -- it will give you percentiles pretty easily!

    SELECT  SalesOrderID, 
        OrderQty,
        RowNum = Row_Number() OVER(Order By OrderQty),
        Rnk = RANK() OVER(ORDER BY OrderQty),
        DenseRnk = DENSE_RANK() OVER(ORDER BY OrderQty),
        NTile4  = NTILE(4) OVER(ORDER BY OrderQty)
    FROM Sales.SalesOrderDetail 
    WHERE SalesOrderID IN (43689, 63181)
    
    0 讨论(0)
  • 2020-12-14 02:43

    How about this:

    SELECT
      Group,
      75_percentile =  MAX(case when NTILE(4) OVER(ORDER BY score ASC) = 3 then score  else 0 end),
      90_percentile =  MAX(case when NTILE(10) OVER(ORDER BY score  ASC) = 9 then score  else 0 end)     
    FROM TheScore
    GROUP BY Group
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题