博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ORACEL学习--分析函数汇总
阅读量:6504 次
发布时间:2019-06-24

本文共 11622 字,大约阅读时间需要 38 分钟。

hot3.png

文章来自: 

      

oracle分析函数汇总

一、分析函数语法

function_name(
,
...) over(
);

function_name():函数名称

argument:参数

over( ):开窗函数

partition_Clause:分区子句,数据记录集分组,group by...

order by_Clause:排序子句,数据记录集排序,order by...

windowing_Clause:开窗子句,定义分析函数在操作行的集合,三种开窗方式:rows、range、Specifying

注:使用开窗子句时一定要有排序子句!!!

本篇未涉及开窗子句,开窗子句在另外的文章中单独说明

一、order by对窗口的影响

  不含order by的:
  SQL> select deptno,sal,sum(sal) over() from emp;
  不含order by时,默认的窗口是从结果集的第一行直到末尾。
  

     含order by的:

  SQL> select deptno,sal, sum(sal) over(order by deptno) as sumsal  from emp;
  当含有order by时,默认的窗口是从第一行直到当前分组的最后一行。
  

二、用于排列的函数

  SQL>

select empno, deptno, sal,        rank()   over (partition by deptno order by sal desc nulls last) as rank,      dense_rank()  over (partition by deptno order by sal desc nulls last) as dense_rank,      row_number()  over(partition by deptno order by sal desc nulls last) as row_numberfrom emp;

  

三、用于合计的函数

  SQL>

select deptno,sal,     sum(sal) over (partition by deptno) as sumsal,     avg(sal) over (partition by deptno) as avgsal,     count(*) over (partition by deptno) as count,     max(sal) over (partition by deptno) as maxsal     from emp;

  

四、开窗语句

  1、rows窗口: "rows 5 preceding"  适用于任何类型而且可以order by多列。

   SQL> select deptno,ename,sal,
            sum(sal) over (order by deptno rows 2 preceding) sumsal
            from emp;
  rows 2 preceding:将当前行和它前面的两行划为一个窗口,因此sum函数就作 用在这三行上面
  

     SQL> select deptno,ename,sal,

            sum(sal) over (partition by deptno order by ename rows 2 preceding) sumsal
            from emp  order by deptno,ename;
  加了partiton by分区后之后,rows 2 preceding(窗口)只在当前分区内生效,不会影响分区之外的行。

  SQL> select ename,sal,hiredate,
  first_value(ename) over  (order by hiredate asc rows 5 preceding) first_ename,
  first_value(hiredate) over (order by hiredate asc rows 5 preceding) first_hiredate
  from emp  order by hiredate asc;
  order by hiredate asc rows 5 preceding: order by之后,取当前行的前5行+当前行作为窗口(共6行)。
  

       2、"range unbounded preceding"

  range unbounded preceding会把当前行之前的所有行都包含进来,但当partition by时:

  SQL> select deptno,ename,sal,
 sum(sal) over (partition by deptno order by deptno range unbounded preceding) sumsal
   from emp;

  SQL> select deptno,ename,sal,
    sum(sal) over (order by deptno range unbounded preceding) sumsal
    from emp;

  这SQL句子和下面这SQL是等价的:
  select deptno,ename,sal, sum(sal) over (order by deptno) sumsal   from emp;
  因为order by的默认窗口总是从结果集的第一行开始到它分组的最后一行。
  而partiton by的默认窗口总是从分区的第一行开始。

  3、range窗口: "range 100 preceding"

  这个子句只适用于number和date,而且只能order by一列。
  如果over()里asc排列,意思是[number-100,number]这样一个闭区间是它的窗口。
  如果over()里desc排列,意思是[number,number+100]这样一个闭区间是它的窗口。

  4、窗口总结
  1、unbounded preceding:从当前分区的第一行开始,到当前行结束。
  2、current row:从当前行开始,也结束于当前行。
  3、[numeric expression] preceding:对于rows来说从当前行之前的第[numeric expression]行开始,到当前行结束。对range来说从小于数值表达式的值开始,到当前行结束。
  4、[numeric expression] following:与[numeric expression] preceding相反。

 

 

 

二、分析函数汇总

1、count() over()  :统计分区中各组的行数,partition by 可选,order by 可选

select ename,esex,eage,count(*) over() from emp; --总计数select ename,esex,eage,count(*) over(order by eage) from emp; --递加计数select ename,esex,eage,count(*) over(partition by esex) from emp; --分组计数select ename,esex,eage,count(*) over(partition by esex order by eage) from emp;--分组递加计数

 

2、sum() over()  :统计分区中记录的总和,partition by 可选,order by 可选

select ename,esex,eage,sum(salary) over() from emp; --总累计求和select ename,esex,eage,sum(salary) over(order by eage) from emp; --递加累计求和select ename,esex,eage,sum(salary) over(partition by esex) from emp; --分组累计求和select ename,esex,eage,sum(salary) over(partition by esex order by eage) from emp; --分组递加累计求和

 

3、avg() over()  :统计分区中记录的平均值,partition by 可选,order by 可选

select ename,esex,eage,avg(salary) over() from emp; --总平均值select ename,esex,eage,avg(salary) over(order by eage) from emp; --递加求平均值select ename,esex,eage,avg(salary) over(partition by esex) from emp; --分组求平均值select ename,esex,eage,avg(salary) over(partition by esex order by eage) from emp; --分组递加求平均值

 

4、min() over() :统计分区中记录的最小值,partition by 可选,order by 可选

     max() over() :统计分区中记录的最大值,partition by 可选,order by 可选

select ename,esex,eage,salary,min(salary) over() from emp; --求总最小值select ename,esex,eage,salary,min(salary) over(order by eage) from emp; --递加求最小值select ename,esex,eage,salary,min(salary) over(partition by esex) from emp; --分组求最小值select ename,esex,eage,salary,min(salary) over(partition by esex order by eage) from emp; --分组递加求最小值

 

5、rank() over()  :跳跃排序,partition by 可选,order by 必选

select ename,eage,rank() over(partition by job order by eage) from emp;select ename,eage,rank() over(order by eage) from emp;

 

6、dense_rank() :连续排序,partition by 可选,order by 必选

select ename,eage,dense_rank() over(partition by job order by eage) from emp;select ename,eage,dense_rank() over(order by eage) from emp;

 

7、row_number() over() :排序,无重复值,partition by 可选,order by 必选

select ename,eage,row_number() over(partition by job order by eage) from emp;select ename,eage,row_number() over(order by eage) from emp;

 

8、ntile(n) over() :partition by 可选,order by 必选

    n表示将分区内记录平均分成n份,多出的按照顺序依次分给前面的组

select ename,salary,ntile(3) over(order by salary desc) from emp;select ename,salary,ntile(3) over(partition by job order by salary desc) from emp;

 

9、first_value() over() :取出分区中第一条记录的字段值,partition by 可选,order by 可选

     last_value() over() :取出分区中最后一条记录的字段值,partition by 可选,order by 可选

select ename,first_value(salary) over() from emp;select ename,first_value(salary) over(order by salary desc) from emp;select ename,first_value(salary) over(partition by job) from emp;                                                           select ename,first_value(salary) over(partition by job order by salary desc) from emp;

 

10、first :从DENSE_RANK返回的集合中取出排在最前面的一个值的行

      last :从DENSE_RANK返回的集合中取出排在最后面的一个值的行

select job,max(salary) keep(dense_rank first order by salary desc),max(salary) keep(dense_rank last order by salary desc) from empgroup by job;

 

11、lag() over() :取出前n行数据,partition by 可选,order by 必选

      lead() over() :取出后n行数据,partition by 可选,order by 必选

select ename,eage,lag(eage,1,0) over(order by salary), lead(eage,1,0) over(order by salary) from emp;select ename,eage,lag(eage,1) over(partition by esex order by salary),lead(eage,1) over(partition by esex order by salary) from emp;

 

12、ratio_to_report(a) over(partition by b) :求按照b分组后a的值在所属分组中总值的占比,a的值必须为数值或数值型字段

      partition by 可选,order by 不可选

select ename,job,salary,ratio_to_report(1) over() from emp; --给每一行赋值1,求当前行在总值的占比,总是0.1select ename,job,salary,ratio_to_report(salary) over() from emp; --当前行的值在所有数据中的占比select ename,job,salary,ratio_to_report(1) over(partition by job) from emp; --给每一行赋值1,求当前行在分组后的组内总值的占比select ename,job,salary,ratio_to_report(salary) over(partition by job) from emp; --当前行的值在分组后组内总值占比

 

13、percent_rank() over()  :partition by 可选,order by 必选

     所在组排名序号-1除以该组所有的行数-1,排名跳跃排序

select ename,job,salary,percent_rank() over(order by salary) from emp;select ename,job,salary,percent_rank() over(partition by job order by salary) from emp;

 

14、cume_dist() over() :partition by 可选,order by必选

所在组排名序号除以该组所有的行数,注意对于重复行,计算时取重复行中的最后一行的位置

select ename,job,salary,cume_dist() over(order by salary) from emp;select ename,job,salary,cume_dist() over(partition by job order by salary) from emp;

 

15、precentile_cont( x ) within group(order by ...) over()    :over()中partition by可选,order by 不可选

x为输入的百分比,是0-1之间的一个小数,返回该百分比位置的数据,若没有则返回以下计算值(r):

a=1+( x *(N-1) )  x为输入的百分比,N为分区内的记录的行数

b=ceil ( a )  向上取整

c = floor( a ) 向下取整

r=a * 百分比位置上一条数据 + b * 百分比位置下一条数据

select ename,job,salary,percentile_cont(0.5) within group(order by salary) over() from emp;select ename,job,salary,percentile_cont(0.5) within group(order by salary) over(partition by job) from emp;

 

16、precentile_disc( x ) within group(order by ...) over()   :over()中partition by可选,order by 不可选

x为输入的百分比,是0-1之间的一个小数,返回百分比位置对应位置上的数据值,若没有对应数据值,就取大于该分布值的下一个值

select ename,job,salary,percentile_disc(0.5) within group(order by salary) over()from emp;select ename,job,salary,percentile_disc(0.5) within group(order by salary) over(partition by job) from emp;

 

17、stddev() over():计算样本标准差,只有一行数据时返回0,partition by 可选,order by 可选

      stddev_samp() over():计算样本标准差,只有一行数据时返回null,partition by 可选,order by 可选

      stddev_pop() over():计算总体标准差,partition by 可选,order by 可选

select stddev(stu_age) over() from student; --计算所有记录的样本标准差select stddev(stu_age) over(order by stu_age) from student; --计算递加的样本标准差select stddev(stu_age) over(partition by stu_major) from student; --计算分组的样本标准差select stddev(stu_age) over(partition by stu_major order by stu_age) from student; --计算分组递加的样本标准差select stddev_samp(stu_age) over() from student; --计算所有记录的样本标准差select stddev_samp(stu_age) over(order by stu_age) from student; --计算递加的样本标准差select stddev_samp(stu_age) over(partition by stu_major) from student; --计算分组的样本标准差select stddev_samp(stu_age) over(partition by stu_major order by stu_age) from student; --计算分组递加的样本标准差select stddev_pop(stu_age) over() from student; --计算所有记录的总体标准差select stddev_pop(stu_age) over(order by stu_age) from student; --计算递加的总体标准差select stddev_pop(stu_age) over(partition by stu_major) from student; --计算分组的总体标准差select stddev_pop(stu_age) over(partition by stu_major order by stu_age) from student;--计算分组递加的总体标准差

 

18、variance() over():计算样本方差,只有一行数据时返回0,partition by 可选,order by 可选

       var_samp() over():计算样本方差,只有一行数据时返回null,partition by 可选,order by 可选

       var_pop() over():计算总体方差,partition by 可选,order by 可选

select variance(stu_age) over() from student; --计算所有记录的样本方差select variance(stu_age) over(order by stu_age) from student; --计算递加的样本方差select variance(stu_age) over(partition by stu_major) from student; --计算分组的样本方差select variance(stu_age) over(partition by stu_major order by stu_age) from student; --计算分组递加的样本方差select var_samp(stu_age) over() from student; --计算所有记录的样本方差select var_samp(stu_age) over(order by stu_age) from student; --计算递加的样本方差select var_samp(stu_age) over(partition by stu_major) from student; --计算分组的样本方差select var_samp(stu_age) over(partition by stu_major order by stu_age) from student; --计算分组递加的样本方差select var_pop(stu_age) over() from student; --记录所有就的总体方差select var_pop(stu_age) over(order by stu_age) from student; --计算递加的总体方差select var_pop(stu_age) over(partition by stu_major) from student; --计算分组的总体方差select var_pop(stu_age) over(partition by stu_major order by stu_age) from student;--计算分组递加的样本方差

 

stddev()=sqrt( variance() )     sqrt()--求开方

stddev_samp()=sqrt( var_samp() )

stddec_pop=sqrt( var_pop() )

 

19、covar_samp over():返回一对表达式的样本协方差,partition by 可选,order by 可选

       covar_pop over(): 返回一堆表达式的总体协方差,partition by 可选,order by 可选

select covar_samp(stu_age,line) over() from student; --计算所有记录的样本协方差select covar_samp(stu_age,line) over(order by stu_age) from student; --计算递加的样本协方差select covar_samp(stu_age,line) over(partition by stu_major) from student; --计算分组的样本协方差select covar_samp(stu_age,line) over(partition by stu_major order by stu_age) from student; --计算分组递加的样本协方差select covar_pop(stu_age,line) over() from student; --计算所有记录的总体协方差select covar_pop(stu_age,line) over(order by stu_age) from student; --计算递加的总体协方差select covar_pop(stu_age,line) over(partition by stu_major) from student; --计算分组的总体协方差select covar_pop(stu_age,line) over(partition by stu_major order by stu_age) from student; --计算分组递加的总体协方差

 

20、corr() over() :返回一对表达式的相关系数,partition by 可选,order by 可选

select corr(stu_age,line) over() from student; --计算所有记录的相关系数select corr(stu_age,line) over(order by stu_age) from student; --计算递加的相关系数select corr(stu_age,line) over(partition by stu_major) from student; --计算分组的相关系数select corr(stu_age,line) over(partition by stu_major order by stu_age) from student; --计算分组递加的相关系数

 

21、REGR_ (Linear Regression) Functions:这些线性回归函数适合最小二乘法回归线,有9个不同的回归函数可使用

转载于:https://my.oschina.net/spinachgit/blog/2248231

你可能感兴趣的文章
EMLS项目推进思考
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
2018-2019-1 20165302 实验五 通讯协议设计
查看>>
快速寻找满足条件的两个数
查看>>
centos6.5安装LNMP
查看>>
操作笔记:catalina.out膨胀太快,分割tomcat 7日志
查看>>
Golang 知识点总结
查看>>
JAVA 8 特性
查看>>
算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
查看>>
iMatrix平台核心功能—权限管理介绍
查看>>
WebService之Axis2快速入门(7): Spring与axis整合发布为WebServic
查看>>
Uliweb查看模板调用关系
查看>>
C#与PHP通信压缩
查看>>
根据经纬度获取时区信息
查看>>
关于 Linux
查看>>
图文解析五大外链误区
查看>>
ios开发之导航控制器的原理
查看>>
《Netkiller Blockchain 手札》Hyperledger Fabric Java SDK Demo
查看>>
Spring cloud 安全部署与性能优化
查看>>
querySelector 和 querySelectorAll区别
查看>>