Oracle 分析函数参考手册
===========================================================
作者: xsb(http://xsb.itpub.net)
发表于:2006.03.01 12:22
分类: DW&BI
出处:http://xsb.itpub.net/post/419/33028
---------------------------------------------------------------
22/06/2005 12:22 FP
Oracle 从 8.1.6 开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同
之处是对于每个组返回多行,而聚合函数对于每个组只返回一行。
下面例子中使用的表来自 Oracle 自带的 HR 用户下的表,如果没有安装该用户,可以在 SYS 用户
下运行$ORACLE_HOME/demo/schema/human_resources/hr_main.sql 来创建。
除本文内容外,你还可参考:
ROLLUP 与 CUBE http://xsb.itpub.net/post/419/29159
分析函数使用例子介绍:http://xsb.itpub.net/post/419/44634
本文如果未指明,缺省是在 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 章
AVG
功能描述:用于计算一个组和数据窗口内表达式的平均值。