26

在 MSSQL 2005 中计算百分位排名(例如第 90 个百分位或中位数分数)的最佳方法是什么?

我希望能够为单个分数列选择第 25、中位数和第 75 个百分位数(最好在单个记录中,以便我可以结合平均值、最大值和最小值)。例如,结果的表输出可能是:

Group  MinScore  MaxScore  AvgScore  pct25  median  pct75
-----  --------  --------  --------  -----  ------  -----
T1     52        96        74        68     76      84
T2     48        98        74        68     75      85
4

8 回答 8

15

我认为这将是最简单的解决方案:

SELECT TOP N PERCENT FROM TheTable ORDER BY TheScore DESC

其中 N = (100 - 所需百分位数)。因此,如果您希望所有行都在第 90 个百分位,您将选择前 10%。

我不确定您所说的“最好在单个记录中”是什么意思。您的意思是计算单个记录的给定分数将落入哪个百分位?例如,您是否希望能够做出诸如“您的分数是 83,这使您处于第 91 个百分位”之类的陈述。?

编辑:好的,我对您的问题进行了更多思考,并提出了这种解释。您是否在问如何计算特定百分位数的截止分数?例如这样的事情:要进入第 90 个百分位,您的分数必须大于 78。

如果是这样,则此查询有效。我不喜欢子查询,所以根据它的用途,我可能会尝试找到一个更优雅的解决方案。但是,它确实返回具有单个分数的单个记录。

-- 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
于 2008-09-17T04:28:03.003 回答
9

查看 NTILE 命令——它会很容易地给你百分位数!

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)
于 2011-06-28T20:42:40.940 回答
2

这个怎么样:

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
于 2012-09-05T23:24:14.127 回答
1

我一直在努力解决这个问题,这就是我到目前为止的想法:

CREATE PROCEDURE [dbo].[TestGetPercentile]

@percentile as float,
@resultval as float output

AS

BEGIN

WITH scores(score, prev_rank, curr_rank, next_rank) AS (
    SELECT dblScore,
        (ROW_NUMBER() OVER ( ORDER BY dblScore ) - 1.0) / ((SELECT COUNT(*) FROM TestScores) + 1)  [prev_rank],
        (ROW_NUMBER() OVER ( ORDER BY dblScore ) + 0.0) / ((SELECT COUNT(*) FROM TestScores) + 1)  [curr_rank],
        (ROW_NUMBER() OVER ( ORDER BY dblScore ) + 1.0) / ((SELECT COUNT(*) FROM TestScores) + 1)  [next_rank]
    FROM TestScores
)

SELECT @resultval = (
    SELECT TOP 1 
    CASE WHEN t1.score = t2.score
        THEN t1.score
    ELSE
        t1.score + (t2.score - t1.score) * ((@percentile - t1.curr_rank) / (t2.curr_rank - t1.curr_rank))
    END
    FROM scores t1, scores t2
    WHERE (t1.curr_rank = @percentile OR (t1.curr_rank < @percentile AND t1.next_rank > @percentile))
        AND (t2.curr_rank = @percentile OR (t2.curr_rank > @percentile AND t2.prev_rank < @percentile))
)

END

然后在另一个存储过程中我这样做:

DECLARE @pct25 float;
DECLARE @pct50 float;
DECLARE @pct75 float;

exec SurveyGetPercentile .25, @pct25 output
exec SurveyGetPercentile .50, @pct50 output
exec SurveyGetPercentile .75, @pct75 output

Select
    min(dblScore) as minScore,
    max(dblScore) as maxScore,
    avg(dblScore) as avgScore,
    @pct25 as percentile25,
    @pct50 as percentile50,
    @pct75 as percentile75
From TestScores

它仍然不能完全满足我的要求。这将获得所有测试的统计数据;而我希望能够从其中包含多个不同测试的 TestScores 表中进行选择,并为每个不同的测试返回相同的统计数据(就像我在问题的示例表中一样)。

于 2008-12-05T00:13:13.427 回答
1

第 50 个百分位数与中位数相同。在计算其他百分位数时,比如第 80 个,将 80% 的数据按升序排序,其余 20% 的数据按降序排序,取两个中间值的平均值。

注意:中位数查询已经存在了很长时间,但不记得我从哪里得到它,我只是修改它来计算其他百分位数。

DECLARE @Temp TABLE(Id INT IDENTITY(1,1), DATA DECIMAL(10,5))

INSERT INTO @Temp VALUES(0)
INSERT INTO @Temp VALUES(2)
INSERT INTO @Temp VALUES(8)
INSERT INTO @Temp VALUES(4)
INSERT INTO @Temp VALUES(3)
INSERT INTO @Temp VALUES(6)
INSERT INTO @Temp VALUES(6)
INSERT INTO @Temp VALUES(6) 
INSERT INTO @Temp VALUES(7)
INSERT INTO @Temp VALUES(0)
INSERT INTO @Temp VALUES(1)
INSERT INTO @Temp VALUES(NULL)


--50th percentile or median
SELECT ((
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 50 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA
                ) AS A
        ORDER BY DATA DESC) + 
        (
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 50 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA DESC
                ) AS A
        ORDER BY DATA ASC)) / 2.0


--90th percentile 
SELECT ((
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 90 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA
                ) AS A
        ORDER BY DATA DESC) + 
        (
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 10 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA DESC
                ) AS A
        ORDER BY DATA ASC)) / 2.0


--75th percentile
SELECT ((
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 75 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA
                ) AS A
        ORDER BY DATA DESC) + 
        (
        SELECT TOP 1 DATA
        FROM   (
                SELECT  TOP 25 PERCENT DATA
                FROM    @Temp
                WHERE   DATA IS NOT NULL
                ORDER BY DATA DESC
                ) AS A
        ORDER BY DATA ASC)) / 2.0
于 2011-06-28T10:21:41.173 回答
0

我可能会使用 sql server 2005

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

或类似的规定。

于 2008-09-17T03:47:33.350 回答
0

我会做类似的事情:

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

这是正确的吗?

于 2008-09-17T03:48:22.443 回答
0

百分位数由下式计算

(Rank -1) /(total_rows -1)当您按升序对值进行排序时。

下面的查询将为您提供介于 0 和 1 之间的百分位数。分数最低的人将有 0 个百分位数。

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
于 2019-03-08T15:37:01.800 回答