----几大分析的函数
分析分析函数之前先来介绍一下开窗子句,开窗子句分为两种:range以及row即是范围开窗以及行开窗两种
range表示以偏移量来开窗,与当前行的偏移量,只能用number和date类型,而且只能有一列
rows表示以行来开窗,什么类型都行,可以多列
第一行是 unbounded preceding,
当前行是 current row,
最后一行是 unbounded following.
range between unbounded preceding and current row ----从开始的到当前行,如果sum则累加
range between unbounded preceding and unbounded following ----所有数据,如果sum则只有一个总和
range between 500 preceding and 500 following ----与当前行数值偏移量为+-500的所有值
rows between 1 preceding and current row ----前一行到当前行
rows between 1 preceding and 5 following ----前一行到当前行后面的五行
rows between unbounded preceding and 5 following ----第一行到后五行
order by 开窗列时,碰到连续相同数值的行的累加当一行处理
----------------------------
select deptno,ename,sal,
first_value(sal) over(partition by deptno) deptsal,
last_value(sal) over(partition by deptno) deptsal2
from emp;
select sal,ratio_to_report(sal) over() from emp;
--------------------求第二大值
create table t (x int);
insert into t values(8);
insert into t values(10);
insert into t values(10);
insert into t values(11);
insert into t values(7);
insert into t values(5);
commit;
----一
select x,
max(x) over(partition by null order by x range between 2 preceding and current row) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
from t;
---二
select x, max(x) over() deptsal, x, min(x) over() deptsa2 from t;
1.max()默认是所有行最大值,min()默认是所有行最小值,可以结合开窗子句
2.lead,lag
lead(value_expr [,offset][,default]) over([query_partition_clause] order by Order_by_clause)
select x,
lead(x,1) over(partition by null order by x ) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
from t;
select x,
lag(x,1) over(partition by null order by x ) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
from t;
3.rank()over()
select x,
rank() over(partition by null order by x ) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
select x,
dense_rank() over(partition by null order by x ) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
4.row()number()
select x,
dense_rank() over(partition by null order by x ) deptsal,
x,
min(x) over(partition by null order by x range between 2 preceding and 2 following) deptsa2
from t;
select deptno, ename, sal, row_number() over(order by null) rn from emp;
select deptno,ename,sal,row_number() over(order by dbms_random.value) rn ----dbms_random.value()也可以
5.加强型分析函数
rollup(deptno,job) = (deptno,job)(deptno)()
cube(deptno,job) = (deptno,job)(deptno)(job)()
---------------------------加强型函数
group by rollup(a,b,c)
转化为
group by (a,b,c)
union all
group by (a,b)
union all
group by (a)
union all
group by()
-------------------ege:
select deptno,sum(sal) from emp group by rollup(deptno,sal);-----最后一行汇总
------------------------
select deptno,sum(sal) from emp group by deptno,sal
union all
select deptno,sum(sal) from emp group by deptno
union all
select null,sum(sal) from emp
order by deptno;
---------------------------
select deptno,job,grouping(deptno),grouping(job),sum(sal) from emp group by rollup(deptno,job);----以0和1区分汇总数据
select deptno,job,sum(sal) from emp group by cube(deptno,job);--- cube(a,b)= (a,b) (a) (b) ()
select deptno,sum(sal) from emp group by rollup(deptno);
select deptno,job,sum(sal) from emp group by rollup(deptno,job);--rollup(a,b) = (a,b)(a)()
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30018455/viewspace-1390404/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30018455/viewspace-1390404/