PERCENT_RANK (Transact-SQL)

计算 SQL Server 2012 中一组行内某行的相对排名。 使用 PERCENT_RANK 计算一个值在查询结果集或分区中的相对位置。 PERCENT_RANK 类似于 CUME_DIST 函数。

语法

PERCENT_RANK( )
    OVER ( [ partition_by_clause ] order_by_clause )

参数

  • OVER ( [ partition_by_clause ] order_by_clause**)**
    partition_by_clause 将由 FROM 子句生成的结果集划分成 RANK 函数适用的分区。 如果未指定,则此函数将查询结果集的所有行视为单个组。 order_by_clause 确定对执行此操作的逻辑顺序。 order_by_clause 是必需的。 不能在 PERCENT_RANK 函数中指定 OVER 语法的 <rows 或 range 子句>。 有关详细信息,请参阅 OVER 子句 (Transact-SQL)

返回类型

float(53)

一般备注

PERCENT_RANK 返回的值范围大于 0 并小于或等于 1。 任何一组中第一行的 PERCENT_RANK 都为 0。 默认情况下包含 NULL 值,且该值被视为最低的可能值。

示例

下面的示例使用 CUME_DIST 函数计算给定部门内每个雇员的薪金百分比。 CUME_DIST 函数返回的值表示薪金低于或等于同一个部门中当前雇员的雇员百分比。 PERCENT_RANK 函数将雇员的薪金在部门内的排名计算为一个百分比。 指定 PARTITION BY 子句来按部门对结果集中的行进行分区。 OVER 子句中的 ORDER BY 子句对每个分区中的行进行排序。 SELECT 语句中的 ORDER BY 子句对整个结果集中的行进行排序。

USE AdventureWorks2012;
GO
SELECT Department, LastName, Rate, 
       CUME_DIST () OVER (PARTITION BY Department ORDER BY Rate) AS CumeDist, 
       PERCENT_RANK() OVER (PARTITION BY Department ORDER BY Rate ) AS PctRank
FROM HumanResources.vEmployeeDepartmentHistory AS edh
    INNER JOIN HumanResources.EmployeePayHistory AS e  
    ON e.BusinessEntityID = edh.BusinessEntityID
WHERE Department IN (N'Information Services',N'Document Control') 
ORDER BY Department, Rate DESC;

下面是结果集:

Department             LastName               Rate                  CumeDist               PctRank
---------------------- ---------------------- --------------------- ---------------------- ----------------------
Document Control       Arifin                 17.7885               1                      1
Document Control       Norred                 16.8269               0.8                    0.5
Document Control       Kharatishvili          16.8269               0.8                    0.5
Document Control       Chai                   10.25                 0.4                    0
Document Control       Berge                  10.25                 0.4                    0
Information Services   Trenary                50.4808               1                      1
Information Services   Conroy                 39.6635               0.9                    0.888888888888889
Information Services   Ajenstat               38.4615               0.8                    0.666666666666667
Information Services   Wilson                 38.4615               0.8                    0.666666666666667
Information Services   Sharma                 32.4519               0.6                    0.444444444444444
Information Services   Connelly               32.4519               0.6                    0.444444444444444
Information Services   Berg                   27.4038               0.4                    0
Information Services   Meyyappan              27.4038               0.4                    0
Information Services   Bacon                  27.4038               0.4                    0
Information Services   Bueno                  27.4038               0.4                    0
(15 row(s) affected)

请参阅

参考

CUME_DIST (Transact-SQL)