首页 > 代码库 > [MSSQL2012]CUME_DIST函数
[MSSQL2012]CUME_DIST函数
CUME_DIST函数以某列作为基准,计算其它行相对于基准行数据的比例、差距比例,比较容易理解
先看下测试数据
DECLARE @TestData TABLE(
ID INT IDENTITY(1,1),
Department VARCHAR(20),
LastName VARCHAR(20),
Rate FLOAT
)
INSERT INTO @TestData(Department,LastName,Rate)
SELECT ‘Document Control‘,‘Arifin‘,17.7885 UNION ALL
SELECT ‘Document Control‘,‘Norred‘,16.8269 UNION ALL
SELECT ‘Document Control‘,‘Kharatishvili‘,16.8269 UNION ALL
SELECT ‘Information Services‘,‘Chai‘,10.25 UNION ALL
SELECT ‘Information Services‘,‘Berge‘,10.25 UNION ALL
SELECT ‘Information Services‘,‘Trenary‘,50.4808 UNION ALL
SELECT ‘Information Services‘,‘Conroy‘,39.6635 UNION ALL
SELECT ‘Information Services‘,‘Ajenstat‘,38.4615 UNION ALL
SELECT ‘Information Services‘,‘Wilson‘,38.4615 UNION ALL
SELECT ‘Information Services‘,‘Connelly‘,32.4519 UNION ALL
SELECT ‘Information Services‘,‘Meyyappan‘,32.4519
SELECT * FROM @TestData
第一个例子,代码比较简单,结果集里出来的一列Cume_Dist
第二个例子,与例1不同的是添加了分组,CUME_DIST被应用到每个组内,即每个组内有一个自己的基准,组内其它成员参照此基准进行运算
还有一个类似的函数,PERCENT_RANK,先猜猜这个函数做什么用?