rank,dense_rank,row_number使用和区别
rank,dense_rank,row_number區別
一:語法(用法):
???? rank() over([partition by col1] order by col2)?
???? dense_rank() over([partition by col1] order by col2)?
???? row_number() over([partition by col1] order by col2)?
???? 其中[partition by col1]可省略。
二:區別
??? 三個分析函數都是按照col1分組內從1開始排序
????
??? row_number() 是沒有重復值的排序(即使兩天記錄相等也是不重復的),可以利用它來實現分頁
??? dense_rank() 是連續排序,兩個第二名仍然跟著第三名
??? rank()?????? 是跳躍拍學,兩個第二名下來就是第四名
????
??? 理論就不多講了,看了案例,一下就明白了
????
SQL> create table t(
? 2?? name varchar2(10),
? 3?? score number(3));
?
Table created
?
SQL> insert into t(name,score)?
? 2?? select '語文',60 from dual union all
? 3?? select '語文',90 from dual union all
? 4?? select '語文',80 from dual union all
? 5?? select '語文',80 from dual union all
? 6?? select '數學',67 from dual union all
? 7?? select '數學',77 from dual union all
? 8?? select '數學',78 from dual union all
? 9?? select '數學',88 from dual union all
?10?? select '數學',99 from dual union all
?11?? select '語文',70 from dual
?12? /
?
10 rows inserted
?
SQL> select * from t;
?
NAME?????? SCORE
---------- -----
語文????????? 60
語文????????? 90
語文????????? 80
語文????????? 80
數學????????? 67
數學????????? 77
數學????????? 78
數學????????? 88
數學????????? 99
語文????????? 70
?
10 rows selected
?
SQL> select name,score,rank() over(partition by name order by score) tt from t;
?
NAME?????? SCORE???????? TT
---------- ----- ----------
數學????????? 67????????? 1
數學????????? 77????????? 2
數學????????? 78????????? 3
數學????????? 88????????? 4
數學????????? 99????????? 5
語文????????? 60????????? 1
語文????????? 70????????? 2
語文????????? 80????????? 3?? <----
語文????????? 80????????? 3?? <----
語文????????? 90????????? 5
?
10 rows selected
?
SQL> select name,score,dense_rank() over(partition by name order by score) tt from t;
?
NAME?????? SCORE???????? TT
---------- ----- ----------
數學????????? 67????????? 1
數學????????? 77????????? 2
數學????????? 78????????? 3
數學????????? 88????????? 4
數學????????? 99????????? 5
語文????????? 60????????? 1
語文????????? 70????????? 2
語文????????? 80????????? 3?? <----
語文????????? 80????????? 3?? <----
語文????????? 90????????? 4
?
10 rows selected
?
SQL> select name,score,row_number() over(partition by name order by score) tt from t;
?
NAME?????? SCORE???????? TT
---------- ----- ----------
數學????????? 67????????? 1
數學????????? 77????????? 2
數學????????? 78????????? 3
數學????????? 88????????? 4
數學????????? 99????????? 5
語文????????? 60????????? 1
語文????????? 70????????? 2
語文????????? 80????????? 3? <----
語文????????? 80????????? 4? <----
語文????????? 90????????? 5
?
10 rows selected
?
SQL> select name,score,rank() over(order by score) tt from t;
?
NAME?????? SCORE???????? TT
---------- ----- ----------
語文????????? 60????????? 1
數學????????? 67????????? 2
語文????????? 70????????? 3
數學????????? 77????????? 4
數學????????? 78????????? 5
語文????????? 80????????? 6
語文????????? 80????????? 6
數學????????? 88????????? 8
語文????????? 90????????? 9
數學????????? 99???????? 10
?
10 rows selected
?
大家應該明白了吧!呵呵!接下來看應用
一:dense_rank------------------查詢每門功課前三名
? select name,score from (select name,score,dense_rank() over(partition by name order by score desc) tt from t) x where x.tt<=3
??
?
NAME?????? SCORE
---------- -----
數學????????? 99
數學????????? 88
數學????????? 78
語文????????? 90
語文????????? 80
語文????????? 80
?
6 rows selected
二:rank------------------語文成績70分的同學是排名第幾。
?? select name,score,x.tt from (select name,score,rank() over(partition by name order by score desc) tt from t) x where x.name='語文' and x.score=70
?
?
NAME?????? SCORE???????? TT
---------- ----- ----------
語文????????? 70????????? 4
????
三:row_number——————分頁查詢
???? select xx.* from (select t.*,row_number() over(order by score desc) rowno from t) xx where xx.rowno between 1 and 3;
?
NAME?????? SCORE????? ROWNO
---------- ----- ----------
數學????????? 99????????? 1
語文????????? 90????????? 2
數學????????? 88????????? 3
總結
以上是生活随笔為你收集整理的rank,dense_rank,row_number使用和区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 系统添加静态路由的方法
- 下一篇: How to write a custo