ROW_NUMBER()OVER() 是Oracle SQL分析函數(shù),主要是用來對要查詢的數(shù)據(jù)分組排序使用。
使用方法
ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col2)
對列col1分組,col2排序操作。
例子:
創(chuàng)新互聯(lián)公司專注骨干網(wǎng)絡(luò)服務(wù)器租用十載,服務(wù)更有保障!服務(wù)器租用,
雅安服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。SQL> SELECT
2 ROW_NUMBER()OVER(PARTITION BY deptno ORDER BY sal, empno) AS rn,
3 empno, ename, sal, deptno
4 FROM emp;
RN EMPNO ENAME SAL DEPTNO---------- ---------- ---------- ---------- ----------
1 7934 MILLER 1300 10
2 7782 CLARK 2450 10
3 7839 KING 5000 10
1 7369 SMITH 800 20
2 7876 ADAMS 1100 20
3 7566 JONES 2975 20
4 7788 SCOTT 3000 20
5 7902 FORD 3000 20
1 7900 JAMES 950 30
2 7521 WARD 1250 30
3 7654 MARTIN 1250 30
RN EMPNO ENAME SAL DEPTNO---------- ---------- ---------- ---------- ----------
4 7844 TURNER 1500 30
5 7499 ALLEN 1600 30
6 7698 BLAKE 2850 3014 rows selected.
同時也可以單獨使用其來對結(jié)果進行排序
可以和order by 對比一下:
SQL> SELECT empno, ename, sal,
2 ROW_NUMBER()OVER(ORDER BY sal, empno) AS rn
3 FROM emp;
EMPNO ENAME SAL RN
---------- ---------- ---------- ----------
7369 SMITH 800 1
7900 JAMES 950 2
7876 ADAMS 1100 3
7521 WARD 1250 4
7654 MARTIN 1250 5
7934 MILLER 1300 6
7844 TURNER 1500 7
7499 ALLEN 1600 8
7782 CLARK 2450 9
7698 BLAKE 2850 10
7566 JONES 2975 11
EMPNO ENAME SAL RN
---------- ---------- ---------- ----------
7788 SCOTT 3000 12
7902 FORD 3000 13
7839 KING 5000 14
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3145491563
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 462 | 4 (25)| 00:00:01 |
| 1 | WINDOW SORT | | 14 | 462 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 462 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
16 consistent gets
1 physical reads
0 redo size
1049 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
SQL> SELECT empno, ename, sal
2 FROM emp
3 ORDER BY sal, empno;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7900 JAMES 950
7876 ADAMS 1100
7521 WARD 1250
7654 MARTIN 1250
7934 MILLER 1300
7844 TURNER 1500
7499 ALLEN 1600
7782 CLARK 2450
7698 BLAKE 2850
7566 JONES 2975
EMPNO ENAME SAL
---------- ---------- ----------
7788 SCOTT 3000
7902 FORD 3000
7839 KING 5000
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 150391907
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 462 | 4 (25)| 00:00:01 |
| 1 | SORT ORDER BY | | 14 | 462 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 462 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
16 consistent gets
1 physical reads
0 redo size
943 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
對比ORDER BY 子句,排序結(jié)果一樣,使用ROW_NUMBER()OVER()函數(shù)可生產(chǎn)RN列,便于在某些列表程序選擇行數(shù)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標題:ROW_NUMBER()OVER()-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://jinyejixie.com/article16/dpehgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、企業(yè)建站、網(wǎng)站排名、面包屑導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站維護
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)