Oracle 分析函数 ROW_NUMBER() 使用


1、row_number() over()排序功能:

(1) row_number() over()分组排序功能:

在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where group by  order by 的执行。

partition by 用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组,它和聚合函数不同的地方在于它能够返回一个分组中的多条记录,而聚合函数一般只有一个反映统计值的记录。

例如:emp,根据部门分组排序。

SQL> select empno,deptno,sal ,row_number() over (partition by deptno order by sal desc) rank from emp;

  EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7782  10    2450  2
      7934  10    1300  3
      7788  20    3000  1
      7902  20    3000  2
      7566  20    2975  3
      7876  20    1100  4
      7369  20    800  5
      7698  30    2850  1
      7499  30    1600  2
      7844  30    1500  3
      7654  30    1250  4
      7521  30    1250  5
      7900  30    950  6
14 rows selected.

(2)对查询结果进行排序:(无分组)

SQL> select empno,deptno,sal ,row_number() over (order by sal desc) rank from emp;

 EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7902  20    3000  2
      7788  20    3000  3
      7566  20    2975  4
      7698  30    2850  5
      7782  10    2450  6
      7499  30    1600  7
      7844  30    1500  8
      7934  10    1300  9
      7521  30    1250 10
      7654  30    1250 11
      7876  20    1100 12
      7900  30    950 13
      7369  20    800 14
14 rows selected.

row_number() over()和rownum差不多,功能更强一点(可以在各个分组内从1开时排序).

2、rank() over()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内).

SQL> select empno,deptno,sal ,rank() over (partition by deptno order by sal desc) rank from emp;

 EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7782  10    2450  2
      7934  10    1300  3
      7788  20    3000  1
      7902  20    3000  1             
      7566  20    2975  3          --跳跃了
      7876  20    1100  4
      7369  20    800  5
      7698  30    2850  1
      7499  30    1600  2
      7844  30    1500  3
      7654  30    1250  4
      7521  30    1250  4
      7900  30    950  6
14 rows selected.

SQL> select empno,deptno,sal ,rank() over (order by sal desc) rank from emp;

 EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7902  20    3000  2
      7788  20    3000  2 
      7566  20    2975  4        --跳跃了
      7698  30    2850  5
      7782  10    2450  6
      7499  30    1600  7
      7844  30    1500  8
      7934  10    1300  9
      7521  30    1250 10
      7654  30    1250 10
      7876  20    1100 12
      7900  30    950 13
      7369  20    800 14
14 rows selected.

3、dense_rank() over()是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的 .

SQL> select empno,deptno,sal ,dense_rank() over (order by sal desc) rank from emp;

  EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7902  20    3000  2
      7788  20    3000  2
      7566  20    2975  3
      7698  30    2850  4
      7782  10    2450  5
      7499  30    1600  6
      7844  30    1500  7
      7934  10    1300  8
      7521  30    1250  9
      7654  30    1250  9
      7876  20    1100 10
      7900  30    950 11
      7369  20    800 12
14 rows selected.

SQL> select empno,deptno,sal ,dense_rank() over (partition by deptno order by sal desc) rank from emp;

 EMPNO    DEPTNO    SAL      RANK
---------- ---------- ---------- ----------
      7839  10    5000  1
      7782  10    2450  2
      7934  10    1300  3
      7788  20    3000  1
      7902  20    3000  1
      7566  20    2975  2    --不跳跃
      7876  20    1100  3
      7369  20    800  4
      7698  30    2850  1
      7499  30    1600  2
      7844  30    1500  3
      7654  30    1250  4
      7521  30    1250  4
      7900  30    950  5
14 rows selected.

使用ROW_NUMBER删除重复数据

---假设表TAB中有a,b,c三列,可以使用下列语句删除a,b,c都相同的重复行。

DELETE FROM (select year,QUARTER,RESULTS,row_number() over(partition by YEAR,QUARTER,RESULTS order by YEAR,QUARTER,RESULTS) AS ROW_NO FROM SALE ) 

WHERE ROW_NO>1 

https://www.bkjia.com/topicnews.aspx?tid=12

本文永久更新链接地址:https://www.bkjia.com/Linux/2018-03/151305.htm

相关内容