適用於:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Microsoft Fabric
中的 SQL 分析端點Microsoft Fabric
中的倉庫Microsoft Fabric 中的 SQL 資料庫
計算 SQL Server 資料列群組中之資料列的相對排名。 使用 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 為必要項目。
<rows or range clause>函式中OVER無法指定語法的 PERCENT_RANK 。 如需詳細資訊,請參閱 SELECT - OVER 子句。
傳回類型
float(53)
備註
傳 PERCENT_RANK 回的值範圍大於 0 且小於或等於 1。 任何集合中的第一個資料列都有 PERCENT_RANK 的 0。
NULL 值預設會包含,並視為最低的可能值。
PERCENT_RANK 不具決定性。 如需詳細資訊,請參閱決定性與非決定性函數。
範例
本文中的程式代碼範例會使用 AdventureWorks2025 或 AdventureWorksDW2025 範例資料庫,您可以從 Microsoft SQL Server 範例和社群專案 首頁下載。
下列範例會使用 函 CUME_DIST 式來計算指定部門內每個員工的工資百分位數。 函式所 CUME_DIST 傳回的值代表工資小於或等於相同部門中目前員工的員工百分比。 此 PERCENT_RANK 函式會以百分比計算部門內員工工資的等級。
PARTITION BY子句是指定來分割部門結果集中的數據列。
ORDER BY子句中的 OVER 子句會排序每個分割區中的數據列。
ORDER BY語句中的 SELECT 子句會排序整個結果集中的數據列。
USE AdventureWorks2022;
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