[原創] 小議rank(),dense_rank(),row_number()使用與區別
SQL> desc t * from t
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
TS VARCHAR2(10)
SQL> select * from t;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
TS VARCHAR2(10)
SQL> select * from t;
ID TS
---------- ----------
1 20061105
1 20061101
1 20061102
1 20061103
1 20061104
1 20061105
1 20061106
1 20061107
2 20061101
2 20061103
2 20061106
2 20061107
2 20061109
2 20061110
3 20061101
3 20061103
3 20061106
3 20061107
3 20061108
3 20061109
3 20061110
3 20061111
---------- ----------
1 20061105
1 20061101
1 20061102
1 20061103
1 20061104
1 20061105
1 20061106
1 20061107
2 20061101
2 20061103
2 20061106
2 20061107
2 20061109
2 20061110
3 20061101
3 20061103
3 20061106
3 20061107
3 20061108
3 20061109
3 20061110
3 20061111
22 rows selected.
SQL>
要取出大於五次,並且第五次的時間的 ID,TS
結果應該為
ID TS
---------- ----------
1 20061105
2 20061109
3 20061108
SQL> select a.id,a.ts from
2 (select id,ts,row_number() over (partition by id order by ts ) as rn from t ) a
3* where a.rn = 5
ID TS
---------- ----------
1 20061105
2 20061109
3 20061108
SQL> select a.id,a.ts from
2 (select id,ts,row_number() over (partition by id order by ts ) as rn from t ) a
3* where a.rn = 5
ID TS
---------- ----------
1 20061105
2 20061109
3 20061108
---------- ----------
1 20061105
2 20061109
3 20061108
SQL> select a.id,a.ts from
2 (select id,ts,rank() over (partition by id order by ts ) as rn from t ) a
3 where a.rn = 5;
2 (select id,ts,rank() over (partition by id order by ts ) as rn from t ) a
3 where a.rn = 5;
ID TS
---------- ----------
1 20061105
1 20061105
2 20061109
3 20061108
SQL> select id,ts,rank() over (partition by id order by ts ) as rn from t ;
---------- ----------
1 20061105
1 20061105
2 20061109
3 20061108
SQL> select id,ts,rank() over (partition by id order by ts ) as rn from t ;
ID TS RN
---------- ---------- ----------
1 20061101 1
1 20061102 2
1 20061103 3
1 20061104 4
1 20061105 5
1 20061105 5
1 20061106 7
1 20061107 8
2 20061101 1
2 20061103 2
2 20061106 3
2 20061107 4
2 20061109 5
2 20061110 6
3 20061101 1
3 20061103 2
3 20061106 3
3 20061107 4
3 20061108 5
3 20061109 6
3 20061110 7
3 20061111 8
---------- ---------- ----------
1 20061101 1
1 20061102 2
1 20061103 3
1 20061104 4
1 20061105 5
1 20061105 5
1 20061106 7
1 20061107 8
2 20061101 1
2 20061103 2
2 20061106 3
2 20061107 4
2 20061109 5
2 20061110 6
3 20061101 1
3 20061103 2
3 20061106 3
3 20061107 4
3 20061108 5
3 20061109 6
3 20061110 7
3 20061111 8
SQL> select a.id,a.ts from
2 (select id,ts,dense_rank() over (partition by id order by ts ) as rn from t ) a
3 where a.rn = 5;
2 (select id,ts,dense_rank() over (partition by id order by ts ) as rn from t ) a
3 where a.rn = 5;
ID TS
---------- ----------
1 20061105
1 20061105
2 20061109
3 20061108
---------- ----------
1 20061105
1 20061105
2 20061109
3 20061108
SQL> select id,ts,dense_rank() over (partition by id order by ts ) as rn from t;
ID TS RN
---------- ---------- ----------
1 20061101 1
1 20061102 2
1 20061103 3
1 20061104 4
1 20061105 5
1 20061105 5
1 20061106 6
1 20061107 7
2 20061101 1
2 20061103 2
2 20061106 3
2 20061107 4
2 20061109 5
2 20061110 6
3 20061101 1
3 20061103 2
3 20061106 3
3 20061107 4
3 20061108 5
3 20061109 6
3 20061110 7
3 20061111 8
SQL> select * from (
2 select * from (
3 select id,ts from (
4 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
5 )
6 where tsrank=5
7 order by ts
8 )
9 union
10 select * from (
11 select id,ts from (
12 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
13 )
14 where tsrank=5
15 order by ts
16 )
17 );
---------- ---------- ----------
1 20061101 1
1 20061102 2
1 20061103 3
1 20061104 4
1 20061105 5
1 20061105 5
1 20061106 6
1 20061107 7
2 20061101 1
2 20061103 2
2 20061106 3
2 20061107 4
2 20061109 5
2 20061110 6
3 20061101 1
3 20061103 2
3 20061106 3
3 20061107 4
3 20061108 5
3 20061109 6
3 20061110 7
3 20061111 8
SQL> select * from (
2 select * from (
3 select id,ts from (
4 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
5 )
6 where tsrank=5
7 order by ts
8 )
9 union
10 select * from (
11 select id,ts from (
12 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
13 )
14 where tsrank=5
15 order by ts
16 )
17 );
ID TS
---------- ----------
1 20061105
2 20061109
3 20061108
---------- ----------
1 20061105
2 20061109
3 20061108
要取出大於五次,並且第五次的時間的 ID,TS ,同時選出前2個ID
SQL>select * from (
2 select id,ts from (
3 select id,ts,row_number() over(partition by id order by ts) as tsrank from t
4 )
5 where tsrank=5
6 order by ts
7* ) where rownum<=2
2 select id,ts from (
3 select id,ts,row_number() over(partition by id order by ts) as tsrank from t
4 )
5 where tsrank=5
6 order by ts
7* ) where rownum<=2
ID TS
---------- ----------
1 20061105
3 20061108
---------- ----------
1 20061105
3 20061108
SQL> select * from (
2 select id,ts from (
3 select id,ts from (
4 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
5 )
6 where tsrank=5
7 )
8 union
9 select * from (
10 select id,ts from (
11 select id,ts,dense_rank() over(partition by id order by ts) as tsrank from t
12 )
13 where tsrank=5
14 )
15 order by ts
16 )
17 where rownum<=2;
ID TS
---------- ----------
1 20061105
3 20061108
---------- ----------
1 20061105
3 20061108
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7364032/viewspace-433009/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 分析函式rank,dense_rank,row_number使用和區別 .函式
- Hive中row_number()、dense_rank()、rank()的區別Hive
- row_number() over,rank() over,dense_rank() over的區別
- 分析函式中rank(),row_number(),dense_rank()的區別函式
- rank,dense_rank,row_number 分析函式函式
- Oracle:Rank,Dense_Rank,Row_Number比較Oracle
- oracle分析函式Rank, Dense_rank, row_numberOracle函式
- 分析函式——排序排列(rank、dense_rank、row_number)函式排序
- oracle 分析函式之(rank()/dense_rank()/row_number())Oracle函式
- SQL SERVER 排序函式ROW_NUMBER、RANK、DENSE_RANK、NTILESQLServer排序函式
- rank() 與dense_rank()分析
- Spark2 Dataset分析函式--排名函式row_number,rank,dense_rank,percent_rankSpark函式
- lead lag rank()over()dense_rank()row_number()over()rollupcube幾大分析函式函式
- 分析函式DENSE_RANK 和 RANK函式
- mysql自動排序函式dense_rank() over()、rank() over()、row_num() over()用法和區別MySql排序函式
- oracle中聚合函式RANK和dense_rank的使用(轉)Oracle函式
- 有關oracle中聚合函式rank和dense_rank的使用Oracle函式
- 【SQL 學習】分析函式之RANK() DENSE_RANK ()SQL函式
- 分析函式rank() row_number函式
- Oracle 中分析函式用法之--rank(),dense_rank(),partition,over()Oracle函式
- 分析函式——keep(dense_rank first/last)函式AST
- SQL語句中的rank () over , row_number() over ,rank_dense ()SQL
- 【Analytic】分析函式之DENSE_RANK函式函式
- Oracle分析函式RANK(),ROW_NUMBER(),LAG()等的使用方法(轉)Oracle函式
- Oracle vs PostgreSQL Develop(14) - 分析函式KEEP DENSE_RANKOracleSQLdev函式
- [原創] sql mode與 enum 型別SQL型別
- Zookeeper的ZAB協議與Paxos協議區別協議
- RANK函式小結函式
- 原創:oracle DML介紹與使用Oracle
- 國密SSL協議與標準TLS協議的區別協議TLS
- PHPCookie與Session的使用與區別PHPCookieSession
- HTTP協議中PUT和POST使用區別HTTP協議
- 原創 Rman備份中obsolete和expired的區別
- TCP協議與UDP協議以及兩者之間的區別TCP協議UDP
- 原聲ajax與jquery ajax請求的區別jQuery
- 微信小程式與APP區別與優劣勢介紹微信小程式APP
- synchronized與Lock的區別與使用詳解synchronized
- mysql與redis的區別與使用場景MySqlRedis