首页 > 代码库 > Oracle 序号函数

Oracle 序号函数

Oracle提供的序号函数:
以emp表为例:
1: rownum 最简单的序号 但是在order by之前就确定值.
select rownum,t.* from emp t order by ename

  行数ROWNUMEMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
1117876ADAMSCLERK77881987-5-231100 20
227499ALLENSALESMAN76981981-2-20160030030
367698BLAKEMANAGER78391981-5-12850 30
477782CLARKMANAGER78391981-6-92450 10
5137902FORDANALYST75661981-12-33000 20
6127900JAMESCLERK76981981-12-3950 30
747566JONESMANAGER78391981-4-22975 20
897839KINGPRESIDENT 1981-11-175000 10
957654MARTINSALESMAN76981981-9-281250140030
10147934MILLERCLERK77821982-1-231300 10
1187788SCOTTANALYST75661987-4-193000 20
1217369SMITHCLERK79021980-12-17800 20
13107844TURNERSALESMAN76981981-9-81500030
1437521WARDSALESMAN76981981-2-22125050030


2: row_number() over( order by 字段名1,字段名2,...字段名n ) 先排序再确定序号.
select row_number() over( order by ename ) as rm, t.* from emp t

   行数RMEMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
117876ADAMSCLERK77881987-5-231100 20
227499ALLENSALESMAN76981981-2-20160030030
337698BLAKEMANAGER78391981-5-12850 30
447782CLARKMANAGER78391981-6-92450 10
557902FORDANALYST75661981-12-33000 20
667900JAMESCLERK76981981-12-3950 30
777566JONESMANAGER78391981-4-22975 20
887839KINGPRESIDENT 1981-11-175000 10
997654MARTINSALESMAN76981981-9-281250140030
10107934MILLERCLERK77821982-1-231300 10
11117788SCOTTANALYST75661987-4-193000 20
12127369SMITHCLERK79021980-12-17800 20
13137844TURNERSALESMAN76981981-9-81500030
14147521WARDSALESMAN76981981-2-22125050030



3: 和上面的不同之处在于PARTITION分区.在每一个小分区内部取序号.
select row_number() over(PARTITION BY deptno order by sal ) as rm, t.* from emp t

   行数RMEMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
117934MILLERCLERK77821982-1-231300 10
227782CLARKMANAGER78391981-6-92450 10
337839KINGPRESIDENT 1981-11-175000 10
417369SMITHCLERK79021980-12-17800 20
527876ADAMSCLERK77881987-5-231100 20
637566JONESMANAGER78391981-4-22975 20
747788SCOTTANALYST75661987-4-193000 20
857902FORDANALYST75661981-12-33000 20
917900JAMESCLERK76981981-12-3950 30
1027521WARDSALESMAN76981981-2-22125050030
1137654MARTINSALESMAN76981981-9-281250140030
1247844TURNERSALESMAN76981981-9-81500030
1357499ALLENSALESMAN76981981-2-20160030030
1467698BLAKEMANAGER78391981-5-12850 30



4: rank()在这里表示针对每个部门员工的工资取序号.
select rank() over(PARTITION BY deptno order by sal ) as rnk, t.* from emp t;

   行数RNKEMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
117934MILLERCLERK77821982-1-231300 10
227782CLARKMANAGER78391981-6-92450 10
337839KINGPRESIDENT 1981-11-175000 10
417369SMITHCLERK79021980-12-17800 20
527876ADAMSCLERK77881987-5-231100 20
637566JONESMANAGER78391981-4-22975 20
747788SCOTTANALYST75661987-4-193000 20
847902FORDANALYST75661981-12-33000 20
917900JAMESCLERK76981981-12-3950 30
1027521WARDSALESMAN76981981-2-22125050030
1127654MARTINSALESMAN76981981-9-281250140030
1247844TURNERSALESMAN76981981-9-81500030
1357499ALLENSALESMAN76981981-2-20160030030
1467698BLAKEMANAGER78391981-5-12850 30



5: 从例子中可以看到dense_rank()和rank()的唯一区别就是:
dense_rank()中并列第二名后是第三名.
rank()中并列第二名后是第四名.
select dense_rank() over(PARTITION BY deptno order by sal ) as dense_rnk, t.* from emp t;

   行数DENSE_RNKEMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
117934MILLERCLERK77821982-1-231300 10
227782CLARKMANAGER78391981-6-92450 10
337839KINGPRESIDENT 1981-11-175000 10
417369SMITHCLERK79021980-12-17800 20
527876ADAMSCLERK77881987-5-231100 20
637566JONESMANAGER78391981-4-22975 20
747788SCOTTANALYST75661987-4-193000 20
847902FORDANALYST75661981-12-33000 20
917900JAMESCLERK76981981-12-3950 30
1027521WARDSALESMAN76981981-2-22125050030
1127654MARTINSALESMAN76981981-9-281250140030
1237844TURNERSALESMAN76981981-9-81500030
1347499ALLENSALESMAN76981981-2-20160030030
1457698BLAKEMANAGER78391981-5-12850 30

Oracle 序号函数