oracle row number (),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

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值