Oracle 分析函数与聚合函数(重点理解)

一、Oracle分析函数的语法与作用:

 rank ( )             over ( [query_partition_clause]order_by_clause )

dense_rank ( ) over ([query_partition_clause] order_by_clause )

rownumber ( )  over ( [query_partition_clause]order_by_clause )

可实现按指定的字段分组排序,对于相同分组字段的结果集进行排序,其中partition by 为分组字段,order by 指定排序字段

over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用

二、Oracle分析函数与聚合函数的区别:

分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行。

  

三、例子:

许多分析函数同时也是聚合函数,比如sum()函数,这样使用就是聚合函数。

 SQL> select deptno,sum(sal) sum_sal fromemp  group by deptno;

 而这样使用就是分析函数。

 SQL> select distinct deptno,sum(sal)over(partition by deptno) sum_sal from emp;

 它们得出的结果是相同的,都是:

1       10     8750

2       20     10875

3       30     9400

  请注意,这里我们用到了distinct 关键字,如果不用distinct,第2个查询将返回14行数据,即emp表的每行记录都将返回一行sum_sal,因为不用distinct的含义是:针对每个雇员计算他/她所在的部门的薪金总数。

SQL> select deptno,sum(sal) over(partition by deptno) sum_sal from emp

 1       10     8750

2       10     8750

3       10     8750

4       20     10875

5       20     10875

6       20     10875

7       20     10875

8       20     10875

9       30     9400

10     30     9400

11     30     9400

12     30     9400

13     30     9400

14     30     9400

 在这个例子中,聚合函数是更好的选择,但在另外一些情形下,我们更应该使用分析函数。

 下面通过几个实例来介绍部分分析函数的用途。

问题1:求出每个部门工资最高的前3名。

 利用我们传统的聚合函数max可以方便地取出工资最高的一个员工,但是取出多个就无能为力了,同样,如果不分组我们可以通过排序取出工资最高的前3名,但无法实现对多个部门的分组。而采用rank聚合函数,可以方便地实现我们的要求。

  

完整的语句如下:

 select * from (

 select deptno,sal,ename, rank()over(partition by deptno order by sal desc) pm from emp)

 where pm<=3

结果为:

 1     10     KING         5000.00   1

2       10     CLARK      2450.00   2

3       10     MILLER    1300.00   3

4       20     SCOTT      3000.00   1

5       20     FORD        3000.00   1

6       20     JONES       2975.00   3

7       30     BLAKE       2850.00   1

8       30     ALLEN       1600.00   2

9       30     TURNER   1500.00   3

我们在开窗函数中使用deptno(部门编号)作为分组标志,并按照sal(工资)倒序排列。 

 注意:RANK()函数有3组,分别是rank, dense_rank, row_number,它们的区别是:

  rank如果出现两个相同的数据,那么后面的数据就会直接跳过这个排名,比如:当第2名和第3名的利润相同时,rank的结果是1,2,2,4;而dense_rank则不会跳过这个排名,结果是1,2,2,3;而row_number哪怕是两个数据完全相同,排名也会不一样,结果是1,2,3,4

  

 dense_rank()

 完整的语句如下:

 select * from (selectdeptno,ename,sal,dense_rank() over(partition by deptno order by sal desc) pmfrom emp) t where pm<=3

 //按部门编号根据工资由高到低排序

 结果:

  1     10     KING         5000.00   1

2       10     CLARK      2450.00   2

3       10     MILLER    1300.00   3

4       20     SCOTT      3000.00   1

5       20     FORD        3000.00   1

6       20     JONES       2975.00   2

7       20     ADAMS    1100.00   3

8       30     BLAKE       2850.00   1

9       30     ALLEN       1600.00   2

10     30     TURNER   1500.00   3

  Rank()

 完整的语句如下:

 select * from (selectdeptno,ename,sal,rank() over(partition by deptno order by sal desc) pm fromemp) t where pm<=3

 //按部门编号根据工资由高到低排序

 结果:

1       10     KING         5000.00   1

2       10     CLARK      2450.00   2

3       10     MILLER    1300.00   3

4       20     SCOTT      3000.00   1

5       20     FORD        3000.00   1

6       20     JONES       2975.00   3

7       30     BLAKE       2850.00   1

8       30     ALLEN       1600.00   2

9       30     TURNER   1500.00   3

Row_number()

 完整的语句如下:

 select * from (selectdeptno,ename,sal,row_number() over(partition by deptno order by sal desc) pmfrom emp) t where pm<=3

 //按部门编号根据工资由高到底排序

 结果:

 1       10     KING         5000.00   1

2       10     CLARK      2450.00   2

3       10     MILLER    1300.00   3

4       20     SCOTT      3000.00   1

5       20     FORD        3000.00   2

6       20     JONES       2975.00   3

7       30     BLAKE       2850.00   1

8       30     ALLEN       1600.00   2

9       30     TURNER   1500.00   3 

猜你喜欢

转载自blog.csdn.net/myflysun/article/details/70477204