本例子都是基于HR用户的表,用户可以用HR用户登陆到ORACLE10G数据库查看运行结果。
有一些是本人个人的理解,不一定完全正确。
1 AVG :
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
得到排序后每一个PARTITION中的数据与前一个和后一个的平均值。
2 CORR
相关系数是变量之间相关程度的指标。样本相关系数用r表示,总体相关系数用ρ表示,相关系数的取值一般介于-1~1之间。相关系数不是等距度量值,而只是一个顺序数据。计算相关系数一般需大样本.
相关系数 又称皮(尔生)氏积矩相关系数,说明两个现象之间相关关系密切程度的统计分析指标。
聚合函数中:
SELECT weight_class, CORR(list_price, min_price)
FROM product_information
GROUP BY weight_class;
WEIGHT_CLASS CORR(LIST_PRICE,MIN_PRICE)
------------ --------------------------
1 .99914795
2 .999022941
3 .998484472
4 .999359909
5 .999536087
分析函数中:
SELECT employee_id, job_id,
TO_CHAR((SYSDATE - hire_date) YEAR TO MONTH ) "Yrs-Mns", salary,
CORR(SYSDATE-hire_date, salary)
OVER(PARTITION BY job_id) AS "Correlation"
FROM employees
WHERE department_id in (50, 80)
ORDER BY job_id, employee_id;
EMPLOYEE_ID JOB_ID Yrs-Mns SALARY Correlation
----------- ---------- ------- ---------- -----------
145 SA_MAN +08-07 14000 .912385598
146 SA_MAN +08-04 13500 .912385598
147 SA_MAN +08-02 12000 .912385598
148 SA_MAN +05-07 11000 .912385598
149 SA_MAN +05-03 10500 .912385598
150 SA_REP +08-03 10000 .80436755
151 SA_REP +08-02 9500 .80436755
152 SA_REP +07-09 9000 .80436755
153 SA_REP +07-01 8000 .80436755
154 SA_REP +06-05 7500 .80436755
155 SA_REP +05-06 7000 .80436755
Correlation显示的是按照job_id PARTITION 的工作年限和工资的相关系数。从结果来看,这个公司每个职位工资和工作年限是标准的线性关系的,呵呵,我们公司可不是这样。呜呜。。
3 COVAR_POP 和COVAR_SAMP
COVAR=covariance(协方差),POP=population(总体) ,SAMP=sample(样本)
COVAR_POP就是总体协方差,COVAR_SAMP 是样本协方差。
聚合函数中:
SELECT job_id,
COVAR_POP(SYSDATE-hire_date, salary) AS covar_pop,
COVAR_SAMP(SYSDATE-hire_date, salary) AS covar_samp
FROM employees
WHERE department_id in (50, 80)
GROUP BY job_id;
JOB_ID COVAR_POP COVAR_SAMP
---------- ----------- -----------
ST_MAN 436092.000 545115.000
SH_CLERK 782717.500 823913.158
SA_MAN 660700.000 825875.000
SA_REP 579988.466 600702.340
ST_CLERK 176577.250 185870.789
分析函数中:
SELECT product_id, supplier_id,
COVAR_POP(list_price, min_price)
OVER (ORDER BY product_id, supplier_id)
AS CUM_COVP,
COVAR_SAMP(list_price, min_price)
OVER (ORDER BY product_id, supplier_id)
AS CUM_COVS
FROM product_information p
WHERE category_id = 29
ORDER BY product_id, supplier_id;
PRODUCT_ID SUPPLIER_ID CUM_COVP CUM_COVS
---------- ----------- ---------- ----------
1774 103088 0
1775 103087 1473.25 2946.5
1794 103096 1702.77778 2554.16667
1825 103093 1926.25 2568.33333
2004 103086 1591.4 1989.25
2005 103086 1512.5 1815
2416 103088 1475.97959 1721.97619
5 COUNT
SELECT last_name, salary,
COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 50 PRECEDING
AND 150 FOLLOWING) AS mov_count FROM employees;
LAST_NAME SALARY MOV_COUNT
------------------------- ---------- ----------
Olson 2100 3
Markle 2200 2
Philtanker 2200 2
Landry 2400 8
Gee 2400 8
Colmenares 2500 10
Patel 2500 10
以上sql语句得到按照工资排序后,比某员工工资少50到大150的员工的人数。
6 CUME_DIST
CUME=cumulative(累计) DIST=distribution(分布)CUME_DIST为累计分布。
SELECT job_id, last_name, salary, CUME_DIST()
OVER (PARTITION BY job_id ORDER BY salary) AS cume_dist
FROM employees
WHERE job_id LIKE 'PU%';
JOB_ID LAST_NAME SALARY CUME_DIST
---------- ------------------------- ---------- ----------
PU_CLERK Colmenares 2500 .2
PU_CLERK Himuro 2600 .4
PU_CLERK Tobias 2800 .6
PU_CLERK Baida 2900 .8
PU_CLERK Khoo 3100 1
PU_MAN Raphaely 11000 1
7 DENSE_RANK
SELECT d.department_name, 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 ('30', '40');
DEPARTMENT_NAME LAST_NAME SALARY DRANK
----------------------- ------------------ ---------- ----------
Purchasing Colmenares 2500 1
Purchasing Himuro 2600 2
Purchasing Tobias 2800 3
Purchasing Baida 2900 4
Purchasing Khoo 3100 5
Purchasing Raphaely 11000 6
Human Resources Marvis 6500
得到按照某个排序规则和分区规则得到的数据,每个数据所在的位置。
8 FIRST ,LAST ,MAX ,MIN
假设我们要显示出每个科室资格最老,和工资最高的人,虽然要分别使用 hirdate和salary字段排序,使用First函数,可以在一条语句中实现
select DEPARTMENT_ID ,
min(FIRST_NAME||'.'||LAST_NAME)
keep ( dense_rank first order by HIRE_DATE ) OLDESTEMP ,
min(FIRST_NAME||'.'||LAST_NAME)
keep ( dense_rank first order by SALARY DESC ) MAXSALARYEMP
from EMPLOYEES
group by DEPARTMENT_ID
我认为:这个SQL语句实际上先order by HIRE_DATE得到每个分组的排列的RANK,通过first得到第一个数值,也就是HIRE_DATE最早的时间,然后通过这个时间,找到对于的员工。说白了,就是通过处理一个字段,查到另一个字段的值,这样就可以得到资格最老的员工了。工资最高也是一样的道理.
DEPARTMENT_ID OLDESTEMP MAXSALARYEMP
------------- ---------------------------------------------- ---------------------------------------
10 Jennifer.Whalen Jennifer.Whalen
20 Pat.Fay Pat.Fay
30 Karen.Colmenares Karen.Colmenares
40 Susan.Mavris Susan.Mavris
50 Steven.Markle TJ.Olson
60 Diana.Lorentz Diana.Lorentz
70 Hermann.Baer Hermann.Baer
80 Amit.Banda Sundita.Kumar
90 Lex.De Haan Lex.De Haan
100 Luis.Popp Luis.Popp
110 Shelley.Higgins William.Gietz
Kimberely.Grant Kimberely.Grant
9 FIRST_VALUE ,LAST_VALUE
为了每一个90部门的员工,查看对应的部门工资最低的员工名字。
SELECT department_id, last_name, salary, FIRST_VALUE(last_name)
OVER (ORDER BY salary ASC ROWS UNBOUNDED PRECEDING) AS lowest_sal
---UNBOUNDED PRECEDING 就是降序排列后在最前面的一行,也就是工资最低的。
FROM (SELECT * FROM employees WHERE department_id = 90
ORDER BY employee_id);
员工名字是按照工资的出来的,FIRST_VALUE 和FIRST的不同是,FIRST_VALUE 是通过比较值来得到对应的last_name的,而FIRST是通过先得到RANK,然后找到FIRST RANK,可以是值,也可以是其他。
DEPARTMENT_ID LAST_NAME SALARY LOWEST_SAL
------------- ------------- ---------- -------------------------
90 Kochhar 17000 Kochhar
90 De Haan 17000 Kochhar
90 King 24000 Kochhar
10 LAG ,LEAD
这个函数是用的最广泛的了,处理前一条或者后一条数据。看一个例子:
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-MAY-95 3100 0
Tobias 24-JUL-97 2800 3100
Baida 24-DEC-97 2900 2800
Himuro 15-NOV-98 2600 2900
Colmenares 10-AUG-99 2500 2600
得到前一条数据的SALARY,值得注意的是:如果还要按照部门来分,类似这样的结果:
DEPARTMENT_ID LAST_NAME HIRE_DATE SALARY PREV_SAL
------------------------- --------- ---------- ----------
1 Khoo 18-MAY-95 3100 0
1 Tobias 24-JUL-97 2800 3100
2 Baida 24-DEC-97 2900 0
2 Himuro 15-NOV-98 2600 2900
2 Colmenares 10-AUG-99 2500 2600
那么就需要把OVER (ORDER BY hire_date) 变成 OVER (PARTITION BY department_id ORDER BY hire_date).
16 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
17 PERCENT_RANK
其实是RANK的一个变体,只不过RANK是返回的数字,而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 ---(1-1/5)=0 (RANK-1)/COUNT
30 Himuro 2600 0.2 ---(2-1/5)=0.2
30 Tobias 2800 0.4 ---(3-1/5)=0.4
30 Baida 2900 0.6 。。
30 Khoo 3100 0.8
30 Raphaely 11000 1
40 Mavris 6500 0
18 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_r from row at RN)
else
(CRN - RN) * (value of expression_r for row at FRN) +
(RN - FRN) * (value of expression_r 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
19 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
20 RANK
不必所说,上文有提到。
21 RATIO_TO_REPORT
功能描述:该函数计算expression_r/(sum(expression_r))的值,它给出相对于总数的百分比,即当前行对sum(expression_r)的贡献。
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
22 REGR_ (Linear Regression) Functions
功能描述:这些线性回归函数适合最小二乘法回归线,有9个不同的回归函数可使用。
REGR_SLOPE:返回斜率,等于COVAR_POP(expr1, expr2) / VAR_POP(expr2)
REGR_INTERCEPT:返回回归线的y截距,等于
AVG(expr1) - REGR_SLOPE(expr1, expr2) * AVG(expr2)
REGR_COUNT:返回用于填充回归线的非空数字对的数目
REGR_R2:返回回归线的决定系数,计算式为:
If VAR_POP(expr2) = 0 then return NULL
If VAR_POP(expr1) = 0 and VAR_POP(expr2) != 0 then return 1
If VAR_POP(expr1) > 0 and VAR_POP(expr2 != 0 then
return POWER(CORR(expr1,expr),2)
REGR_AVGX:计算回归线的自变量(expr2)的平均值,去掉了空对(expr1, expr2)后,等于AVG(expr2)
REGR_AVGY:计算回归线的应变量(expr1)的平均值,去掉了空对(expr1, expr2)后,等于AVG(expr1)
REGR_SXX: 返回值等于REGR_COUNT(expr1, expr2) * VAR_POP(expr2)
REGR_SYY: 返回值等于REGR_COUNT(expr1, expr2) * VAR_POP(expr1)
REGR_SXY: 返回值等于REGR_COUNT(expr1, expr2) * COVAR_POP(expr1, expr2)
23 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
24 STDDEV
功能描述:计算当前行关于组的标准偏离。(Standard Deviation)
SAMPLE:下例返回部门30按雇佣日期排序的薪水值的累积标准偏离
SELECT last_name, hire_date,salary,
STDDEV(salary) OVER (ORDER BY hire_date) "StdDev"
FROM employees
WHERE department_id = 30;
LAST_NAME HIRE_DATE SALARY StdDev
------------------------- ---------- ---------- ----------
Raphaely 07-12月-94 11000 0
Khoo 18-5月 -95 3100 5586.14357
Tobias 24-7月 -97 2800 4650.0896
Baida 24-12月-97 2900 4035.26125
Himuro 15-11月-98 2600 3649.2465
Colmenares 10-8月 -99 2500 3362.58829
25 STDDEV_POP
功能描述:该函数计算总体标准偏离,并返回总体变量的平方根,其返回值与VAR_POP函数的平方根相同。(Standard Deviation-Population)
SAMPLE:下例返回部门20、30、60的薪水值的总体标准偏差
SELECT department_id, last_name, salary,
STDDEV_POP(salary) OVER (PARTITION BY department_id) AS pop_std
FROM employees
WHERE department_id in (20,30,60);
DEPARTMENT_ID LAST_NAME SALARY POP_STD
------------- ------------------------- ---------- ----------
20 Hartstein 13000 3500
20 Fay 6000 3500
30 Raphaely 11000 3069.6091
30 Khoo 3100 3069.6091
30 Baida 2900 3069.6091
30 Colmenares 2500 3069.6091
30 Himuro 2600 3069.6091
30 Tobias 2800 3069.6091
60 Hunold 9000 1722.32401
60 Ernst 6000 1722.32401
60 Austin 4800 1722.32401
60 Pataballa 4800 1722.32401
60 Lorentz 4200 1722.32401
26 STDDEV_SAMP
该函数计算累积样本标准偏离,并返回总体变量的平方根,其返回值与VAR_POP函数的平方根相同。(Standard Deviation-Sample)
SAMPLE:下例返回部门20、30、60的薪水值的样本标准偏差
SELECT department_id, last_name, hire_date, salary,
STDDEV_SAMP(salary) OVER
(PARTITION BY department_id ORDER BY hire_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sdev
FROM employees
WHERE department_id in (20,30,60);
DEPARTMENT_ID LAST_NAME HIRE_DATE SALARY CUM_SDEV
------------- ------------------------- ---------- ---------- ----------
20 Hartstein 17-2月 -96 13000
20 Fay 17-8月 -97 6000 4949.74747
30 Raphaely 07-12月-94 11000
30 Khoo 18-5月 -95 3100 5586.14357
30 Tobias 24-7月 -97 2800 4650.0896
30 Baida 24-12月-97 2900 4035.26125
30 Himuro 15-11月-98 2600 3649.2465
30 Colmenares 10-8月 -99 2500 3362.58829
60 Hunold 03-1月 -90 9000
60 Ernst 21-5月 -91 6000 2121.32034
60 Austin 25-6月 -97 4800 2163.33077
60 Pataballa 05-2月 -98 4800 1982.42276
60 Lorentz 07-2月 -99 4200 1925.61678
27 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
28 VAR_POP
功能描述:(Variance Population)该函数返回非空集合的总体变量(忽略null),VAR_POP进行如下计算:
(SUM(expr2) - SUM(expr)2 / COUNT(expr)) / COUNT(expr)
SAMPLE:下例计算1998年每月销售的累积总体和样本变量(本例在SH用户下运行)
SELECT t.calendar_month_desc,
VAR_POP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Pop",
VAR_SAMP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Samp"
FROM sales s, times t
WHERE s.time_id = t.time_id AND t.calendar_year = 1998
GROUP BY t.calendar_month_desc;
CALENDAR Var_Pop Var_Samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321E+11 1.2264E+12
1998-03 4.7058E+11 7.0587E+11
1998-04 4.6929E+11 6.2572E+11
1998-05 1.5524E+12 1.9405E+12
1998-06 2.3711E+12 2.8453E+12
1998-07 3.7464E+12 4.3708E+12
1998-08 3.7852E+12 4.3260E+12
1998-09 3.5753E+12 4.0222E+12
1998-10 3.4343E+12 3.8159E+12
1998-11 3.4245E+12 3.7669E+12
29 VAR_SAMP
功能描述:(Variance Sample)该函数返回非空集合的样本变量(忽略null),VAR_POP进行如下计算:
(SUM(expr*expr)-SUM(expr)*SUM(expr)/COUNT(expr))/(COUNT(expr)-1)
SAMPLE:下例计算1998年每月销售的累积总体和样本变量
SELECT t.calendar_month_desc,
VAR_POP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Pop",
VAR_SAMP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Samp"
FROM sales s, times t
WHERE s.time_id = t.time_id AND t.calendar_year = 1998
GROUP BY t.calendar_month_desc;
CALENDAR Var_Pop Var_Samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321E+11 1.2264E+12
1998-03 4.7058E+11 7.0587E+11
1998-04 4.6929E+11 6.2572E+11
1998-05 1.5524E+12 1.9405E+12
1998-06 2.3711E+12 2.8453E+12
1998-07 3.7464E+12 4.3708E+12
1998-08 3.7852E+12 4.3260E+12
1998-09 3.5753E+12 4.0222E+12
1998-10 3.4343E+12 3.8159E+12
1998-11 3.4245E+12 3.7669E+12
1998-12 4.8937E+12 5.3386E+12
30 VARIANCE
功能描述:该函数返回表达式的变量,Oracle计算该变量如下:
如果表达式中行数为1,则返回0
如果表达式中行数大于1,则返回VAR_SAMP
SAMPLE:下例返回部门30按雇佣日期排序的薪水值的累积变化
SELECT last_name, salary, VARIANCE(salary)
OVER (ORDER BY hire_date) "Variance"
FROM employees
WHERE department_id = 30;
LAST_NAME SALARY Variance
------------------------- ---------- ----------
Raphaely 11000 0
Khoo 3100 31205000
Tobias 2800 21623333.3
Baida 2900 16283333.3
Himuro 2600 13317000
Colmenares 2500 11307000