Share via


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 子句產生的結果集,分割成函數所要套用的資料分割。 如未指定,此函數會將查詢結果集的所有資料列視為單一群組。 order_by_clause 可決定執行作業的邏輯順序。 order_by_clause 是必要項目。 不可在 PERCENT_RANK 函數中指定 OVER 語法的 <資料列或範圍子句>。 如需詳細資訊,請參閱<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)