日期:2014-05-16 浏览次数:20431 次
?
=============================================
作者:
xsb([url]http://xsb.itpub.net)[/url]
发表于:2006.03.01 12:22
分类: DW&BI
出处:http://xsb.itpub.net/post/419/33028
---------------------------
???? Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,
而聚合函数对于每个组只返回一行。
常用的分析函数如下所列:
row_number() over(partition by ... order by ...)
rank()
over(partition by ... order by ...)
dense_rank() over(partition by ... order
by ...)
count() over(partition by ... order by ...)
max() over(partition
by ... order by ...)
min() over(partition by ... order by ...)
sum()
over(partition by ... order by ...)
avg() over(partition by ... order by
...)
first_value() over(partition by ... order by ...)
last_value()
over(partition by ... order by ...)
lag() over(partition by ... order by
...)
lead() over(partition by ... order by ...)
???
下面例子中使用的表来自Oracle自带的HR用户下的表,如果没有安装该用户,可以在SYS用户下运行
$ORACLE_HOME/demo/schema/human_resources/hr_main.sql来创建。
除本文内容外,你还可参考:
ROLLUP与CUBE
[url]http://xsb.itpub.net/post/419/29159[/url]
分析函数使用例子介绍:[url]http://xsb.itpub.net/post/419/44634[/url]
本文如果未指明,缺省是在HR用户下运行例子。
开窗函数的的理解:
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
over(order
by salary) 按照salary排序进行累计,order by是个默认的开窗函数
over(partition by
deptno)按照部门分区
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)
主要参考资料:《expert one-on-one》 Tom Kyte 《Oracle9i SQL Reference》第6章
ohwww 2007-3-12 09:19
?
续
70。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
71。CORR
功能描述:返回一对表达式的相关系数,它是如下的缩写:
COVAR_POP(expr1,expr2)/STDDEV_POP(expr1)*STDDEV_POP(expr2))
从统计上讲,相关性是变量之间关联的强度,变量之间的关联意味着在某种程度
上一个变量的值可由其它的值进行预测。通过返回一个-1~1之间的一个数,
相关
系数给出了关联的强度,0表示不相关。
SAMPLE:下例返回1998年月销售收入和月单位销售的关系的累积系数(本例在SH用户下运行)
SELECT t.calendar_month_number,
CORR (SUM(s.amount_sold),
SUM(s.quantity_sold))
OVER (ORDER BY t.calendar_month_number) as
CUM_CORR
FROM sales s, times t
WHERE s.time_id = t.time_id AND
calendar_year = 1998
GROUP BY t.calendar_month_number
ORDER BY
t.calendar_month_number;
CALENDAR_MONTH_NUMBER CUM_CORR
--------------------- ----------
1
2
1
3 .994309382
4 .852040875
5 .846652204
6 .871250628
7
.910029803
8 .917556399
9 .920154356
10 .86720251
11
.844864765
12 .903542662
72。COVAR_POP
功能描述:返回一对表达式的总体协方差。
SAMPLE:下例CUM_COVP返回定价和最小产品价格的累积总体协方差
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