Oracle 分析函數 ROW_NUMBER() 使用

來源:互聯網
上載者:User

標籤:彙總   統計   https   group by   有一個   www   false   log   分組   

1、row_number() over()排序功能:


(1) row_number() over()分組排序功能:

     在使用 row_number() over()函數時候,over()裡頭的分組以及排序的執行晚於 where group by  order by 的執行。


partition by 用於給結果集分組,如果沒有指定那麼它把整個結果集作為一個分組,它和彙總函式不同的地方在於它能夠返回一個分組中的多條記錄,而彙總函式一般只有一個反映統計值的記錄。


例如:emp,根據部門分組排序。


SQL> select empno,deptno,sal ,row_number() over (partition by deptno order by sal desc) rank from emp;


   EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7782   10    2450  2      7934   10    1300  3      7788   20    3000  1      7902   20    3000  2      7566   20    2975  3      7876   20    1100  4      7369   20     800  5      7698   30    2850  1      7499   30    1600  2      7844   30    1500  3      7654   30    1250  4      7521   30    1250  5      7900   30     950  614 rows selected.


(2)對查詢結果進行排序:(無分組)


SQL> select empno,deptno,sal ,row_number() over (order by sal desc) rank from emp;


 EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7902   20    3000  2      7788   20    3000  3      7566   20    2975  4      7698   30    2850  5      7782   10    2450  6      7499   30    1600  7      7844   30    1500  8      7934   10    1300  9      7521   30    1250 10      7654   30    1250 11      7876   20    1100 12      7900   30     950 13      7369   20     800 1414 rows selected.


row_number() over()和rownum差不多,功能更強一點(可以在各個分組內從1開時排序).


2、rank() over()是跳躍排序,有兩個第二名時接下來就是第四名(同樣是在各個分組內).


SQL> select empno,deptno,sal ,rank() over (partition by deptno order by sal desc) rank from emp;


 EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7782   10    2450  2      7934   10    1300  3      7788   20    3000  1      7902   20    3000  1                    7566   20    2975  3          --跳躍了      7876   20    1100  4      7369   20     800  5      7698   30    2850  1      7499   30    1600  2      7844   30    1500  3      7654   30    1250  4      7521   30    1250  4      7900   30     950  614 rows selected.


SQL> select empno,deptno,sal ,rank() over (order by sal desc) rank from emp;


 EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7902   20    3000  2      7788   20    3000  2         7566   20    2975  4        --跳躍了      7698   30    2850  5      7782   10    2450  6      7499   30    1600  7      7844   30    1500  8      7934   10    1300  9      7521   30    1250 10      7654   30    1250 10      7876   20    1100 12      7900   30     950 13      7369   20     800 1414 rows selected.



3、dense_rank() over()是連續排序,有兩個第二名時仍然跟著第三名。相比之下row_number是沒有重複值的 .



SQL> select empno,deptno,sal ,dense_rank() over (order by sal desc) rank from emp;


   EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7902   20    3000  2      7788   20    3000  2      7566   20    2975  3      7698   30    2850  4      7782   10    2450  5      7499   30    1600  6      7844   30    1500  7      7934   10    1300  8      7521   30    1250  9      7654   30    1250  9      7876   20    1100 10      7900   30     950 11      7369   20     800 1214 rows selected.


SQL> select empno,deptno,sal ,dense_rank() over (partition by deptno order by sal desc) rank from emp;


EMPNO     DEPTNO     SAL       RANK---------- ---------- ---------- ----------      7839   10    5000  1      7782   10    2450  2      7934   10    1300  3      7788   20    3000  1      7902   20    3000  1      7566   20    2975  2    --不跳躍      7876   20    1100  3      7369   20     800  4      7698   30    2850  1      7499   30    1600  2      7844   30    1500  3      7654   30    1250  4      7521   30    1250  4      7900   30     950  514 rows selected.




使用ROW_NUMBER重複資料刪除資料 

---假設表TAB中有a,b,c三列,可以使用下列語句刪除a,b,c都相同的重複行。


DELETE FROM (select year,QUARTER,RESULTS,row_number() over(partition by YEAR,QUARTER,RESULTS order by YEAR,QUARTER,RESULTS) AS ROW_NO FROM SALE )   

WHERE ROW_NO>1  



參考:https://www.cnblogs.com/alsf/p/6344197.html



Oracle 分析函數 ROW_NUMBER() 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.