Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行,利用这一特性,我们可以对待分析的数据打很多分析标识字段(TOPN/RANK,AVG,MIN,LAG,LEAD),利用这些额外的字段,就可以很方便的实现一些复杂统计分析功能,而且其执行性能要比传统的sql实现要高不少,这一特性,在olap环境中的统计分析中尤为有用。总之一句话:分析函数,很好,很强大!
分析函数命令格式
Function名称([参数]) OVER ([partition子句][ order子句] [window子句])
OVER为分析函数的关键字,用于区别普通的聚合函数;从语法格式上区分的话,没加over()即时聚合函数,加了over()就是分析函数。
Partition子句:Partition by exp1[ ,exp2]...;
主要用于分组,可以理解成select中的group by;不过它跟select语句后跟的group by子句并不冲突;指定该子句之后,前面的函数起效范围就是该分组内,若不指定,则Function的起效范围是全部结果集。
Order子句:Order by exp1[asc|desc] [ ,exp2 [asc|desc]]... [nulls first|last];
其参数基本与select中的order by相同;Nulls first|last是用来限定nulls在分组序列中的所在位置的,我们知道oracle中对于null的定义是未知,所以默认order by的时候nulls总会被排在最前面。如果想控制值为null的行显示位置,nulls first|last参数就能派上用场了。
Window子句:该子句的语法比较复杂,具体可以见下图;
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
over()每行对应的数据窗口是整个记录集
over(order by salary rowsbetween 50 preceding andcurrent row)
每行对应的数据窗口是之前50行到当前行
over(order by salary range between 50 preceding and 150 following)
每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150
over(order by salary rows between 50 preceding and 150 following)
每行对应的数据窗口是之前50行,之后150行
over(order by salary rows between unbounded preceding and unbounded following)
每行对应的数据窗口是从第一行到最后一行,等效:
over(order by salary range between unbounded preceding and unbounded following)
下面例子中使用的表来自Oracle自带的HR用户下的表,如果没有安装该用户,可以在SYS用户下运行
$ORACLE_HOME/demo/schema/human_resources/hr_main.sql来创建。
少数几个例子需要访问SH用户下的表,如果没有安装该用户,可以在SYS用户下运行
$ORACLE_HOME/demo/schema/sales_history/sh_main.sql来创建。
如果未指明缺省是在HR用户下运行例子。
常用分析函数介绍
AVG
功能描述:用于计算一个组和数据窗口内表达式的平均值。
SAMPLE:下面的例子中列c_mavg计算员工表中每个员工的平均薪水报告,该平均值由当前员工和与之具有相同
经理的前一个和后一个三者的平均数得来;
SELECT manager_id, last_name, hire_date, salary,
AVG(salary) OVER (PARTITION BY manager_id ORDER BY hire_date
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS c_mavg
FROM employees;
MANAGER_ID LAST_NAME HIRE_DATE SALARY C_MAVG
---------- ------------------------- --------- ---------- ----------
100 Kochhar 21-SEP-89 17000 17000
100 De Haan 13-JAN-93 17000 15000
100 Raphaely 07-DEC-94 11000 11966.6667
100 Kaufling 01-MAY-95 7900 10633.3333
100 Hartstein 17-FEB-96 13000 9633.33333
100 Weiss 18-JUL-96 8000 11666.6667
100 Russell 01-OCT-96 14000 11833.3333
下面对比用传统的sql和分析函数来实现同样的功能,其执行成本的大小
SQL> explain plan for select a.employee_id,a.salary,a.department_id,avg_salary from employees a,
( select department_id,avg(salary) avg_salary from employees group by department_id) b
where a.department_id=b.department_id order by a.department_id;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1883277112
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 339 | 9153 | 8 (25)| 00:00:0
| 1 | MERGE JOIN | | 339 | 9153 | 8 (25)| 00:00:0
| 2 | SORT JOIN | | 11 | 176 | 4 (25)| 00:00:0
| 3 | VIEW | | 11 | 176 | 4 (25)| 00:00:0
| 4 | HASH GROUP BY | | 11 | 77 | 4 (25)| 00:00:0
| 5 | TABLE ACCESS FULL| EMPLOYEES | 107 | 749 | 3 (0)| 00:00:0
|* 6 | SORT JOIN | | 107 | 1177 | 4 (25)| 00:00:0
| 7 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0)| 00:00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID")
filter("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID")
20 rows selected
用传统的sql执行其cost=8,再看下面的分析函数执行cost=4,可以看到,效率明显提高
SQL> explain plan for select employee_id,salary,department_id,avg(salary) over (partition by department_id) from employees ;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1919783947
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 107 | 1177 | 4 (25)| 00:00:01 |
| 1 | WINDOW SORT | | 107 | 1177 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMPLOYEES | 107 | 1177 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------
.
MAX
功能描述:在一个组中的数据窗口中查找表达式的最大值。
SAMPLE:下面例子中dept_max返回当前行所在部门的最大薪水值
****不需要order by子句,该函数会默认在每一个partition分组中求最大值
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
MIN
功能描述:在一个组中的数据窗口中查找表达式的最小值。
SAMPLE:下面例子中dept_min返回当前行所在部门的最小薪水值
****不需要order by子句,该函数会默认在每一个分组中求最小值
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
SUM
功能描述:该函数计算组中表达式的累积和。
SAMPLE:下例计算同一经理下员工的薪水累积值
SELECT manager_id, last_name, salary,
SUM (salary) OVER (PARTITION BY manager_id ORDER BY salary
RANGE UNBOUNDED PRECEDING) l_csum
FROM employees
WHERE manager_id in (101,103,108);
MANAGER_ID LAST_NAME SALARY L_CSUM
---------- ------------------------- ---------- ----------
101 Whalen 4400 4400
101 Mavris 6500 10900
101 Baer 10000 20900
101 Greenberg 12000 44900
101 Higgins 12000 44900
103 Lorentz 4200 4200
103 Austin 4800 13800
103 Pataballa 4800 13800
103 Ernst 6000 19800
108 Popp 6900 6900
108 Sciarra 7700 14600
108 Urman 7800 22400
108 Chen 8200 30600
108 Faviet 9000 39600
***sum,avg,max, min函数如果不加order子句,窗口为每个partition分组内的数据记录集合,如果加了order子句,则默认窗口为rows between unbounded preceding and current row,结果为
SQL> select last_name,department_id,salary,sum(salary) over(partition by department_id order by rowid ) s from employees where department_id in (20,30);
LAST_NAME DEPARTMENT_ID SALARY S
------------------------- ------------- ---------- ----------
Hartstein 20 13000.00 13000
Fay 20 6000.00 19000
Raphaely 30 11000.00 11000
Khoo 30 3100.00 14100
Baida 30 2900.00 17000
Tobias 30 2800.00 19800
Himuro 30 2600.00 22400
Colmenares 30 2500.00 24900
如果需要求改组所有的值得sum,则需要显示的改变order的窗口,增加 rows between unbounded preceding and unbounded following,如下
SQL> select last_name,department_id,salary,sum(salary) over(partition by department_id order by rowid rows between unbounded preceding and unbounded following) s from employees where department_id in (20,30);
或者干脆不要order by子句
LAST_NAME DEPARTMENT_ID SALARY S
------------------------- ------------- ---------- ----------
Hartstein 20 13000.00 19000
Fay 20 6000.00 19000
Raphaely 30 11000.00 24900
Khoo 30 3100.00 24900
Baida 30 2900.00 24900
Tobias 30 2800.00 24900
Himuro 30 2600.00 24900
Colmenares 30 2500.00 24900
COUNT
功能描述:对一组内发生的事情进行累积计数,如果指定*或一些非空常数,count将对所有行计数,如果指定一个表达式,count返回表达式非空赋值的计数,当有相同值出现时,这些相等的值都会被纳入被计算的值;可以使用DISTINCT来记录去掉一组中完全相同的数据后出现的行数。
SAMPLE:下面例子中计算每个员工在按薪水排序中当前行附近薪水在[n-50,n+150]之间的行数,n表示当前行的薪水
例如,Philtanker的薪水2200,排在他之前的行中薪水大于等于2200-50的有1行,排在他之后的行中薪水小于等于2200+150的行没有,所以count计数值cnt3为2(包括自己当前行);cnt2值相当于小于等于当前行的SALARY值的所有行数
SELECT last_name, salary, COUNT(*) OVER () AS cnt1,
COUNT(*) OVER (ORDER BY salary) AS cnt2,
COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 50 PRECEDING
AND 150 FOLLOWING) AS cnt3 FROM employees;
LAST_NAME SALARY CNT1 CNT2 CNT3
------------------------- ---------- ---------- ---------- ----------
Olson 2100 107 1 3
Markle 2200 107 3 2
Philtanker 2200 107 3 2
Landry 2400 107 5 8
Gee 2400 107 5 8
Colmenares 2500 107 11 10
Patel 2500 107 11 10
.
DENSE_RANK
功能描述:根据ORDER BY子句中表达式的值,从查询返回的每一行,计算它们与其它行的相对位置。组内的数据按ORDER BY子句排序,然后给每一行赋一个号,从而形成一个序列,该序列从1开始,往后累加。每次ORDER BY表达式的值发生变化时,该序列也随之增加。有同样值的行得到同样的数字序号(认为null时相等的)。密集的序列返回的时没有间隔的数
SAMPLE:下例中计算每个员工按部门分区再按薪水排序,依次出现的序列号(注意与RANK函数的区别)
SELECT d.department_id , e.last_name, e.salary, DENSE_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 3
60 Hunold 9000 4
90 Kochhar 17000 1
90 De Haan 17000 1
90 King 24000 2
FIRST
功能描述:从DENSE_RANK返回的集合中取出排在最前面的一个值的行(可能多行,因为值可能相等),因此完整的语法需要在开始处加上一个集合函数以从中取出记录
SAMPLE:下面例子中DENSE_RANK按部门分区,再按佣金commission_pct排序,FIRST取出佣金最低的对应的所有行,然后前面的MAX函数从这个集合中取出薪水最低的值;LAST取出佣金最高的对应的所有行,然后前面的MIN函数从这个集合中取出薪水最高的值
SELECT last_name, department_id, salary,
MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
OVER (PARTITION BY department_id) "Worst",
MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
OVER (PARTITION BY department_id) "Best"
FROM employees
WHERE department_id in (20,80)
ORDER BY department_id, salary;
LAST_NAME DEPARTMENT_ID SALARY Worst Best
------------------------- ------------- ---------- ---------- ----------
Fay 20 6000 6000 13000
Hartstein 20 13000 6000 13000
Kumar 80 6100 6100 14000
Banda 80 6200 6100 14000
Johnson 80 6200 6100 14000
Ande 80 6400 6100 14000
Lee 80 6800 6100 14000
Tuvault 80 7000 6100 14000
Sewall 80 7000 6100 14000
Marvins 80 7200 6100 14000
Bates 80 7300 6100 14000
..
FIRST_VALUE
功能描述:返回组中数据窗口的第一个值。
SAMPLE:下面例子计算按部门分区按薪水排序的数据窗口的第一个值对应的名字,如果薪水的第一个值有多个,则从多个对应的名字中取缺省排序的第一个名字
SELECT department_id, last_name, salary, FIRST_VALUE(last_name)
OVER (PARTITION BY department_id ORDER BY salary ASC ) AS lowest_sal
FROM employees
WHERE department_id in(20,30);
DEPARTMENT_ID LAST_NAME SALARY LOWEST_SAL
------------- ------------------------- ---------- --------------
20 Fay 6000 Fay
20 Hartstein 13000 Fay
30 Colmenares 2500 Colmenares
30 Himuro 2600 Colmenares
30 Tobias 2800 Colmenares
30 Baida 2900 Colmenares
30 Khoo 3100 Colmenares
30 Raphaely 11000 Colmenares
LAG
功能描述:可以访问结果集中的其它行而不用进行自连接。它允许去处理游标,就好像游标是一个数组一样。在给定组中可参考当前行之前的行,这样就可以从组中与当前行一起选择以前的行。Offset是一个正整数,其默认值为1,若索引超出窗口的范围,就返回默认值(默认返回的是组中第一行),第二个参数表示之前值不存在时候的默认值.其相反的函数是LEAD
SAMPLE:下面的例子中列prev_sal返回按hire_date排序的前1行的salary值
SELECT last_name, hire_date, salary,
LAG(salary, 1, 0) OVER (ORDER BY hire_date) AS prev_sal
FROM employees
WHERE job_id = 'PU_CLERK';
LAST_NAME HIRE_DATE SALARY PREV_SAL
------------------------- ---------- ---------- ----------
Khoo 18-5月-95 3100 0
Tobias 24-7月-97 2800 3100
Baida 24-12月-97 2900 2800
Himuro 15-11月-98 2600 2900
Colmenares 10-8月-99 2500 2600
LAST
功能描述:从DENSE_RANK返回的集合中取出排在最后面的一个值的行(可能多行,因为值可能相等),因此完整的语法需要在开始处加上一个集合函数以从中取出记录
SAMPLE:下面例子中DENSE_RANK按部门分区,再按佣金commission_pct排序,FIRST取出佣金最低的对应的所有行,然后前面的MAX函数从这个集合中取出薪水最低的值;LAST取出佣金最高的对应的所有行,然后前面的MIN函数从这个集合中取出薪水最高的值
SELECT last_name, department_id, salary,
MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
OVER (PARTITION BY department_id) "Worst",
MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
OVER (PARTITION BY department_id) "Best"
FROM employees
WHERE department_id in (20,80)
ORDER BY department_id, salary;
LAST_NAME DEPARTMENT_ID SALARY Worst Best
------------------------- ------------- ---------- ---------- ----------
Fay 20 6000 6000 13000
Hartstein 20 13000 6000 13000
Kumar 80 6100 6100 14000
Banda 80 6200 6100 14000
Johnson 80 6200 6100 14000
Ande 80 6400 6100 14000
Lee 80 6800 6100 14000
Tuvault 80 7000 6100 14000
Sewall 80 7000 6100 14000
Marvins 80 7200 6100 14000
Bates 80 7300 6100 14000
.
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
****last_value()函数的默认的窗口是范围是rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,在进行比较的时候从当前窗口组中的第一行开始向后进行比较,所以会出现上边的结果。
把窗口改为rows between unbounded preceding and unbounded following即可。
SELECT department_id, last_name, salary, LAST_VALUE(last_name)
OVER(PARTITION BY department_id ORDER BY salary rows Between unbounded preceding And unbounded following) AS highest_sal
FROM employees
WHERE department_id in(20,30)
DEPARTMENT_ID LAST_NAME SALARY HIGHEST_SAL
------------- ------------------------- ---------- -------------------------
20 Fay 6000.00 Hartstein
20 Hartstein 13000.00 Hartstein
30 Colmenares 2500.00 Raphaely
30 Himuro 2600.00 Raphaely
30 Tobias 2800.00 Raphaely
30 Baida 2900.00 Raphaely
30 Khoo 3100.00 Raphaely
30 Raphaely 11000.00 Raphaely
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
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
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
ROW_NUMBER
功能描述:返回有序组中一行的偏移量,从而可用于按特定标准排序的行号。
SAMPLE:下例返回每个员工再在每个部门中按员工号排序后的顺序号
SELECT department_id, last_name, employee_id, ROW_NUMBER()
OVER (PARTITION BY department_id ORDER BY employee_id) AS emp_id
FROM employees
WHERE department_id < 50;
DEPARTMENT_ID LAST_NAME EMPLOYEE_ID EMP_ID
------------- ------------------------- ----------- ----------
10 Whalen 200 1
20 Hartstein 201 1
20 Fay 202 2
30 Raphaely 114 1
30 Khoo 115 2
30 Baida 116 3
30 Tobias 117 4
30 Himuro 118 5
30 Colmenares 119 6
40 Mavris 203 1