日期:2014-05-16  浏览次数:20391 次

OVER(PARTITION BY)函数介绍

OVER(PARTITION BY)函数介绍

开窗函数??????????
???? Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行。

????? 开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
1:over后的写法:????
?? over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数
?? over(partition by deptno)按照部门分区

?

?? over(partition by deptno order by salary)

?

2:开窗的窗口范围
over(order by salary?range?between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。

举例:

?

--sum(s)over(order by s range between 2 preceding and 2 following)?表示加2或2的范围内的求和

?select name,class,s, sum(s)over(order by s range between 2 preceding and 2 following) mm from t2
adf??????? 3??????? 45??????? 45? --45加2减2即43到47,但是s在这个范围内只有45
asdf?????? 3??????? 55??????? 55
cfe??????? 2??????? 74??????? 74
3dd??????? 3??????? 78??????? 158 --78在76到80范围内有78,80,求和得158
fda??????? 1??????? 80??????? 158
gds??????? 2??????? 92??????? 92
ffd??????? 1??????? 95??????? 190
dss??????? 1??????? 95??????? 190
ddd??????? 3??????? 99??????? 198

gf???????? 3??????? 99??????? 198

?

?