Oracle 9i 分析函式參考手冊(5)

jss001發表於2009-04-06

PERCENT_RANK
功能描述:和CUME_DIST(累積分配)函式類似,對於一個組中給定的行來說,在計算那行的序號時,先減1,然後除以n-1(n為組中所有的行數)。該函式總是返回0~1(包括1)之間的數。
SAMPLE:下例中如果Khoo的salary為2900,則pr值為0.6,因為RANK函式對於等值的返回序列值是一樣的

SELECT department_id, last_name, salary,
PERCENT_RANK()
OVER (PARTITION BY department_id ORDER BY salary) AS pr
FROM employees
WHERE department_id < 50
ORDER BY department_id,salary;

DEPARTMENT_ID LAST_NAME SALARY PR
------------- ------------------------- ---------- ----------
10 Whalen 4400 0
20 Fay 6000 0
20 Hartstein 13000 1
30 Colmenares 2500 0
30 Himuro 2600 0.2
30 Tobias 2800 0.4
30 Baida 2900 0.6
30 Khoo 3100 0.8
30 Raphaely 11000 1
40 Mavris 6500 0


PERCENTILE_CONT
功能描述:返回一個與輸入的分佈百分比值相對應的資料值,分佈百分比的計算方法見函式PERCENT_RANK,如果沒有正好對應的資料值,就透過下面演算法來得到值:
RN = 1+ (P*(N-1)) 其中P是輸入的分佈百分比值,N是組內的行數
CRN = CEIL(RN) FRN = FLOOR(RN)
if (CRN = FRN = RN) then
(value of expression from row at RN)
else
(CRN - RN) * (value of expression for row at FRN) +
(RN - FRN) * (value of expression for row at CRN)
注意:本函式與PERCENTILE_DISC的區別在找不到對應的分佈值時返回的替代值的計算方法不同

SAMPLE:在下例中,對於部門60的Percentile_Cont值計算如下:
P=0.7 N=5 RN =1+ (P*(N-1)=1+(0.7*(5-1))=3.8 CRN = CEIL(3.8)=4
FRN = FLOOR(3.8)=3
(4 - 3.8)* 4800 + (3.8 - 3) * 6000 = 5760

SELECT last_name, salary, department_id,
PERCENTILE_CONT(0.7) WITHIN GROUP (ORDER BY salary)
OVER (PARTITION BY department_id) "Percentile_Cont",
PERCENT_RANK()
OVER (PARTITION BY department_id ORDER BY salary) "Percent_Rank"
FROM employees WHERE department_id IN (30, 60);

LAST_NAME SALARY DEPARTMENT_ID Percentile_Cont Percent_Rank
------------------------- ---------- ------------- --------------- ------------
Colmenares 2500 30 3000 0
Himuro 2600 30 3000 0.2
Tobias 2800 30 3000 0.4
Baida 2900 30 3000 0.6
Khoo 3100 30 3000 0.8
Raphaely 11000 30 3000 1
Lorentz 4200 60 5760 0
Austin 4800 60 5760 0.25
Pataballa 4800 60 5760 0.25
Ernst 6000 60 5760 0.75
Hunold 9000 60 5760 1


PERCENTILE_DISC
功能描述:返回一個與輸入的分佈百分比值相對應的資料值,分佈百分比的計算方法見函式CUME_DIST,如果沒有正好對應的資料值,就取大於該分佈值的下一個值。
注意:本函式與PERCENTILE_CONT的區別在找不到對應的分佈值時返回的替代值的計算方法不同

SAMPLE:下例中0.7的分佈值在部門30中沒有對應的Cume_Dist值,所以就取下一個分佈值0.83333333所對應的SALARY來替代

SELECT last_name, salary, department_id,
PERCENTILE_DISC(0.7) WITHIN GROUP (ORDER BY salary )
OVER (PARTITION BY department_id) "Percentile_Disc",
CUME_DIST() OVER (PARTITION BY department_id ORDER BY salary) "Cume_Dist"
FROM employees
WHERE department_id in (30, 60);

LAST_NAME SALARY DEPARTMENT_ID Percentile_Disc Cume_Dist
------------------------- ---------- ------------- --------------- ----------
Colmenares 2500 30 3100 .166666667
Himuro 2600 30 3100 .333333333
Tobias 2800 30 3100 .5
Baida 2900 30 3100 .666666667
Khoo 3100 30 3100 .833333333
Raphaely 11000 30 3100 1
Lorentz 4200 60 6000 .2
Austin 4800 60 6000 .6
Pataballa 4800 60 6000 .6
Ernst 6000 60 6000 .8
Hunold 9000 60 6000 1


RANK
功能描述:根據ORDER BY子句中表示式的值,從查詢返回的每一行,計算它們與其它行的相對位置。組內的資料按ORDER BY子句排序,然後給每一行賦一個號,從而形成一個序列,該序列從1開始,往後累加。每次ORDER BY表示式的值發生變化時,該序列也隨之增加。有同樣值的行得到同樣的數字序號(認為null時相等的)。然而,如果兩行的確得到同樣的排序,則序數將隨後跳躍。若兩行序數為1,則沒有序數2,序列將給組中的下一行分配值3,DENSE_RANK則沒有任何跳躍。
SAMPLE:下例中計算每個員工按部門分割槽再按薪水排序,依次出現的序列號(注意與DENSE_RANK函式的區別)

SELECT d.department_id , e.last_name, e.salary, RANK()
OVER (PARTITION BY e.department_id ORDER BY e.salary) as drank
FROM employees e, departments d
WHERE e.department_id = d.department_id
AND d.department_id IN ('60', '90');

DEPARTMENT_ID LAST_NAME SALARY DRANK
------------- ------------------------- ---------- ----------
60 Lorentz 4200 1
60 Austin 4800 2
60 Pataballa 4800 2
60 Ernst 6000 4
60 Hunold 9000 5
90 Kochhar 17000 1
90 De Haan 17000 1
90 King 24000 3
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18921899/viewspace-1017190/,如需轉載,請註明出處,否則將追究法律責任。

相關文章