Oracle提供的序號函數
以emp表為例:
1: rownum 最簡單的序號 但是在order by之前就確定值.
select rownum,t.* from emp t order by ename
行數 ROWNUM EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 11 7876 ADAMS CLERK 7788 1987-5-23 1100 20
2 2 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
3 6 7698 BLAKE MANAGER 7839 1981-5-1 2850 30
4 7 7782 CLARK MANAGER 7839 1981-6-9 2450 10
5 13 7902 FORD ANALYST 7566 1981-12-3 3000 20
6 12 7900 JAMES CLERK 7698 1981-12-3 950 30
7 4 7566 JONES MANAGER 7839 1981-4-2 2975 20
8 9 7839 KING PRESIDENT 1981-11-17 5000 10
9 5 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
10 14 7934 MILLER CLERK 7782 1982-1-23 1300 10
11 8 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
12 1 7369 SMITH CLERK 7902 1980-12-17 800 20
13 10 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
14 3 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
2: row_number() over( order by 欄位名1,欄位名2,...欄位名n ) 先排序再確定序號.
select row_number() over( order by ename ) as rm, t.* from emp t
行數 RM EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 1 7876 ADAMS CLERK 7788 1987-5-23 1100 20
2 2 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
3 3 7698 BLAKE MANAGER 7839 1981-5-1 2850 30
4 4 7782 CLARK MANAGER 7839 1981-6-9 2450 10
5 5 7902 FORD ANALYST 7566 1981-12-3 3000 20
6 6 7900 JAMES CLERK 7698 1981-12-3 950 30
7 7 7566 JONES MANAGER 7839 1981-4-2 2975 20
8 8 7839 KING PRESIDENT 1981-11-17 5000 10
9 9 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
10 10 7934 MILLER CLERK 7782 1982-1-23 1300 10
11 11 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
12 12 7369 SMITH CLERK 7902 1980-12-17 800 20
13 13 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
14 14 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
3: 和上面的不同之處在於PARTITION分區.在每一個小分區內部取序號.
select row_number() over(PARTITION BY deptno order by sal ) as rm, t.* from emp t
行數 RM EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 1 7934 MILLER CLERK 7782 1982-1-23 1300 10
2 2 7782 CLARK MANAGER 7839 1981-6-9 2450 10
3 3 7839 KING PRESIDENT 1981-11-17 5000 10
4 1 7369 SMITH CLERK 7902 1980-12-17 800 20
5 2 7876 ADAMS CLERK 7788 1987-5-23 1100 20
6 3 7566 JONES MANAGER 7839 1981-4-2 2975 20
7 4 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
8 5 7902 FORD ANALYST 7566 1981-12-3 3000 20
9 1 7900 JAMES CLERK 7698 1981-12-3 950 30
10 2 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
11 3 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
12 4 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
13 5 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
14 6 7698 BLAKE MANAGER 7839 1981-5-1 2850 30
4: rank()在這裡表示針對每個部門員工的工資取序號.
select rank() over(PARTITION BY deptno order by sal ) as rnk, t.* from emp t;
行數 RNK EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 1 7934 MILLER CLERK 7782 1982-1-23 1300 10
2 2 7782 CLARK MANAGER 7839 1981-6-9 2450 10
3 3 7839 KING PRESIDENT 1981-11-17 5000 10
4 1 7369 SMITH CLERK 7902 1980-12-17 800 20
5 2 7876 ADAMS CLERK 7788 1987-5-23 1100 20
6 3 7566 JONES MANAGER 7839 1981-4-2 2975 20
7 4 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
8 4 7902 FORD ANALYST 7566 1981-12-3 3000 20
9 1 7900 JAMES CLERK 7698 1981-12-3 950 30
10 2 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
11 2 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
12 4 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
13 5 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
14 6 7698 BLAKE MANAGER 7839 1981-5-1 2850 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_RNK EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 1 7934 MILLER CLERK 7782 1982-1-23 1300 10
2 2 7782 CLARK MANAGER 7839 1981-6-9 2450 10
3 3 7839 KING PRESIDENT 1981-11-17 5000 10
4 1 7369 SMITH CLERK 7902 1980-12-17 800 20
5 2 7876 ADAMS CLERK 7788 1987-5-23 1100 20
6 3 7566 JONES MANAGER 7839 1981-4-2 2975 20
7 4 7788 SCOTT ANALYST 7566 1987-4-19 3000 20
8 4 7902 FORD ANALYST 7566 1981-12-3 3000 20
9 1 7900 JAMES CLERK 7698 1981-12-3 950 30
10 2 7521 WARD SALESMAN 7698 1981-2-22 1250 500 30
11 2 7654 MARTIN SALESMAN 7698 1981-9-28 1250 1400 30
12 3 7844 TURNER SALESMAN 7698 1981-9-8 1500 0 30
13 4 7499 ALLEN SALESMAN 7698 1981-2-20 1600 300 30
14 5 7698 BLAKE MANAGER 7839 1981-5-1 2850 30