Share via


CUME_DIST (Transact-SQL)

計算 SQL Server 2012 中之值群組內某值的累計分佈。 亦即,CUME_DIST 會計算值群組中某值的相對位置。 以資料列 r 為例,假設此資料列採取遞增排序,r 的 CUME_DIST 即是具有值小於或等於 r 值之資料列的數量,除以資料分割或查詢結果集中計算所得之資料列數的結果。 CUME_DIST 與 PERCENT_RANK 函數類似。

語法

CUME_DIST( )
    OVER ( [ partition_by_clause ] order_by_clause )

引數

  • OVER ( [ partition_by_clause ] order_by_clause**)**
    partition_by_clause 會將 FROM 子句產生的結果集,分割成函數所要套用的資料分割。 如未指定,此函數會將查詢結果集的所有資料列視為單一群組。 order_by_clause 可決定執行作業的邏輯順序。 order_by_clause 是必要項目。 CUME_DIST 函數不可指定 OVER 語法的 <資料列或範圍子句>。 如需詳細資訊,請參閱<OVER 子句 (Transact-SQL)>。

傳回類型

float(53)

一般備註

CUME_DIST 傳回的值範圍大於 0 並小於或等於 1。 繫結值的計算所得一律會是相同的累計分佈值。 預設會包含 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)

請參閱

參考

PERCENT_RANK (Transact-SQL)