110个oracle常用函数大全(81-90)

发布时间:2021-01-17编辑:脚本学堂
110个oracle常用函数大全(81-90)

81. LAST_VALUE
功能描述:返回组中数据窗口的最后一个值. 
SAMPLE:下面例子计算按部门分区按薪水排序的数据窗口的最后一个值对应的名字,如果薪水的最后一个值有多个,则从多个对应的名字中取缺省排序的最后一个名字
SELECT department_id, last_name, salary, LAST_VALUE(last_name)
OVER(PARTITION BY department_id ORDER BY salary) AS highest_sal
FROM employees
WHERE department_id in(20,30);
DEPARTMENT_ID LAST_NAME SALARY HIGHEST_SAL
------------- ------------------------- ---------- ------------
20 Fay 6000 Fay
20 Hartstein 13000 Hartstein
30 Colmenares 2500 Colmenares
30 Himuro 2600 Himuro
30 Tobias 2800 Tobias
30 Baida 2900 Baida
30 Khoo 3100 Khoo
30 Raphaely 11000 Raphaely
82. LEAD
功能描述:LEAD与LAG相反,LEAD可以访问组中当前行之后的行. Offset是一个正整数,其默认值为1,若索引超出窗口的范围,就返回默认值(默认返回的是组中第一行)
SAMPLE:下面的例子中每行的"NextHired"返回按hire_date排序的下一行的hire_date值
SELECT last_name, hire_date,
LEAD(hire_date, 1) OVER (ORDER BY hire_date) AS "NextHired"
FROM employees WHERE department_id = 30;
LAST_NAME HIRE_DATE NextHired
------------------------- --------- ---------
Raphaely 07-DEC-94 18-MAY-95
Khoo 18-MAY-95 24-JUL-97
Tobias 24-JUL-97 24-DEC-97
Baida 24-DEC-97 15-NOV-98
Himuro 15-NOV-98 10-AUG-99
Colmenares 10-AUG-99

83. MAX
功能描述:在一个组中的数据窗口中查找表达式的最大值. 
SAMPLE:下面例子中dept_max返回当前行所在部门的最大薪水值
SELECT department_id, last_name, salary,
MAX(salary) OVER (PARTITION BY department_id) AS dept_max
FROM employees WHERE department_id in (10,20,30);
DEPARTMENT_ID LAST_NAME SALARY DEPT_MAX
------------- ------------------------- ---------- ----------
10 Whalen 4400 4400
20 Hartstein 13000 13000
20 Fay 6000 13000
30 Raphaely 11000 11000
30 Khoo 3100 11000
30 Baida 2900 11000
30 Tobias 2800 11000
30 Himuro 2600 11000
30 Colmenares 2500 11000

84. MIN
功能描述:在一个组中的数据窗口中查找表达式的最小值. 
SAMPLE:下面例子中dept_min返回当前行所在部门的最小薪水值
SELECT department_id, last_name, salary,
MIN(salary) OVER (PARTITION BY department_id) AS dept_min
FROM employees WHERE department_id in (10,20,30);
DEPARTMENT_ID LAST_NAME SALARY DEPT_MIN
------------- ------------------------- ---------- ----------
10 Whalen 4400 4400
20 Hartstein 13000 6000
20 Fay 6000 6000
30 Raphaely 11000 2500
30 Khoo 3100 2500
30 Baida 2900 2500
30 Tobias 2800 2500
30 Himuro 2600 2500
30 Colmenares 2500 2500

85. NTILE
功能描述:将一个组分为"表达式"的散列表示,例如,如果表达式=4,则给组中的每一行分配一个数(从1到4),如果组中有20行,则给前5行分配1,给下5行分配2等等. 如果组的基数不能由表达式值平均分开,则对这些行进行分配时,组中就没有任何percentile的行数比其它percentile的行数超过一行,最低的percentile是那些拥有额外行的percentile. 例如,若表达式=4,行数=21,则percentile=1的有5行,percentile=2的有5行等等. 
SAMPLE:下例中把6行数据分为4份
SELECT last_name, salary,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile FROM employees
WHERE department_id = 100;
LAST_NAME SALARY QUARTILE
------------------------- ---------- ----------
Greenberg 12000 1
Faviet 9000 1
Chen 8200 2
Urman 7800 2
Sciarra 7700 3
Popp 6900 4

86. 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

87. 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

88. 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

89. 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

90. RATIO_TO_REPORT
功能描述:该函数计算expression/(sum(expression))的值,它给出相对于总数的百分比,即当前行对sum(expression)的贡献. 
SAMPLE:下例计算每个员工的工资占该类员工总工资的百分比
SELECT last_name, salary, RATIO_TO_REPORT(salary) OVER () AS rr
FROM employees
WHERE job_id = 'PU_CLERK';
LAST_NAME SALARY RR
------------------------- ---------- ----------
Khoo 3100 .223021583
Baida 2900 .208633094
Tobias 2800 .201438849
Himuro 2600 .18705036
Colmenares 2500 .179856115