Oracle并列排名显示

第一种:dense_rank() over (order  by  字段  升序或降序)  as  别名  from 表名;

使用别名方便之后过滤条件使用;

排名的时候并列算同一个人,如,1,2,2,3

SQL> select ename,job,sal,dense_rank() over(order by sal desc) as rank from emp;

ENAME      JOB              SAL       RANK
---------- --------- ---------- ----------
KING       PRESIDENT       5000          1
FORD       ANALYST         3000          2
SCOTT      ANALYST         3000          2
JONES      MANAGER         2975          3
BLAKE      MANAGER         2850          4
CLARK      MANAGER         2450          5
ALLEN      SALESMAN        1600          6
TURNER     SALESMAN        1500          7
ADAMS      CLERK           1400          8
SMITH      CLERK           1400          8
MILLER     CLERK           1300          9
WARD       SALESMAN        1250         10
MARTIN     SALESMAN        1250         10
JAMES      CLERK            950         11

已选择14行。
比如限制条件为 排名第二的员工,并列的都显示出来:
SQL> select * from (select ename,job,sal,dense_rank() over(order by sal desc) as rank from emp) where rank =2;

ENAME      JOB              SAL       RANK
---------- --------- ---------- ----------
SCOTT      ANALYST         3000          2
FORD       ANALYST         3000          2

第二种:rank() over (order  by  字段名  升序或者降序) as  别名  from 表名;

排名的时候,并列多少个人,序号就算多少个,如:1,2,2,4

SQL> select ename,job,sal,rank() over(order by sal desc) as rank from emp;

ENAME      JOB              SAL       RANK
---------- --------- ---------- ----------
KING       PRESIDENT       5000          1
FORD       ANALYST         3000          2
SCOTT      ANALYST         3000          2
JONES      MANAGER         2975          4
BLAKE      MANAGER         2850          5
CLARK      MANAGER         2450          6
ALLEN      SALESMAN        1600          7
TURNER     SALESMAN        1500          8
ADAMS      CLERK           1400          9
SMITH      CLERK           1400          9
MILLER     CLERK           1300         11
WARD       SALESMAN        1250         12
MARTIN     SALESMAN        1250         12
JAMES      CLERK            950         14

已选择14行。

第三种:row_number() over (order by  字段名  升序或降序)  as  别名  from 表名;

这种就是不分并列,直接按序号来排名,如:1,2,3,4

SQL> select ename,job,sal,row_number() over(order by sal desc) as rank from emp;

ENAME      JOB              SAL       RANK
---------- --------- ---------- ----------
KING       PRESIDENT       5000          1
FORD       ANALYST         3000          2
SCOTT      ANALYST         3000          3
JONES      MANAGER         2975          4
BLAKE      MANAGER         2850          5
CLARK      MANAGER         2450          6
ALLEN      SALESMAN        1600          7
TURNER     SALESMAN        1500          8
ADAMS      CLERK           1400          9
SMITH      CLERK           1400         10
MILLER     CLERK           1300         11
WARD       SALESMAN        1250         12
MARTIN     SALESMAN        1250         13
JAMES      CLERK            950         14

已选择14行。


你可能感兴趣的:(Oracle)