第一種: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行。 ———————————————— 版權聲明:本文爲CSDN博主「奧利奧小兵」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處連接及本聲明。 原文連接:https://blog.csdn.net/WXB52112181314/article/details/80838567