Oracle Rank() Over()

Rank()使用說明:

 

a. 函數簡介:

    返回結果集分區內指定字段的值的排名,指定字段的值的排名是相關行之前的排名加一。

b. 語法:

    RANK() OVER([<partiton_by_clause>]<order by clause>)

c. 參數說明:

    partition_by_clause 將from子句生成的結果集劃分爲應用到RANK函數的分區。

    Order_by_clause確定將RANK值應用到分區中的行時所使用的順序。

d. 以下是實例使用:

 

 

1. 創建測試表

 

Sql代碼  收藏代碼
  1. --創建表  
  2. -- Create table  
  3. create table T_SCORE  
  4. (  
  5.   AUTOID   NUMBER not null,  
  6.   S_ID     NUMBER(3),  
  7.   S_NAME   CHAR(8) not null,  
  8.   SUB_NAME VARCHAR2(20),  
  9.   SCORE    NUMBER(10,2)  
  10. );  
  11. -- Add comments to the table   
  12. comment on table T_SCORE  
  13.   is '學生成績表';  
  14. -- Add comments to the columns   
  15. comment on column T_SCORE.AUTOID  
  16.   is '主鍵ID';  
  17. comment on column T_SCORE.S_ID  
  18.   is '學生ID';  
  19. comment on column T_SCORE.S_NAME  
  20.   is '學生姓名';  
  21. comment on column T_SCORE.SUB_NAME  
  22.   is '科目';  
  23. comment on column T_SCORE.SCORE  
  24.   is '成績';  

 

2. 創建測試記錄

 

Sql代碼  收藏代碼
  1. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  2. values (8, 1, '張三    ''語文', 80.00);  
  3.   
  4. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  5. values (9, 2, '李四    ''數學', 80.00);  
  6.   
  7. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  8. values (10, 1, '張三    ''數學', 0.00);  
  9.   
  10. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  11. values (11, 2, '李四    ''語文', 50.00);  
  12.   
  13. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  14. values (12, 3, '張三丰  ''語文', 10.00);  
  15.   
  16. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  17. values (13, 3, '張三丰  ''數學'null);  
  18.   
  19. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  20. values (14, 3, '張三丰  ''體育', 120.00);  
  21.   
  22. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  23. values (15, 4, '楊過    ''JAVA', 90.00);  
  24.   
  25. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  26. values (16, 5, 'mike    ''c++', 80.00);  
  27.   
  28. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  29. values (3, 3, '張三丰  ''Oracle', 0.00);  
  30.   
  31. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  32. values (4, 4, '楊過    ''Oracle', 77.00);  
  33.   
  34. insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE)  
  35. values (17, 2, '李四    ''Oracle', 77.00);  

 

3. 分不同情況查詢

3.1 查詢所有的學生成績

 

Sql代碼  收藏代碼
  1. --1.查詢所有的學生成績  
  2. select t.s_id 學號, t.s_name 姓名, t.sub_name 科目, t.score 成績  
  3.   from t_score t;  

查詢結果:

學號 姓名 科目 成績
1 張三     語文 80.00
2 李四     數學 80.00
1 張三     數學 0.00
2 李四     語文 50.00
3 張三丰   語文 10.00
3 張三丰   數學  
3 張三丰   體育 120.00
4 楊過     JAVA 90.00
5 mike     c++ 80.00
3 張三丰   Oracle 0.00
4 楊過     Oracle 77.00
2 李四     Oracle 77.00

 

3.2 查詢Oracle科目成績名次-非連續rank

Sql代碼  收藏代碼
  1. --2.查詢Oracle科目成績名次-非連續rank  
  2. select t.s_id 學號,  
  3.        t.s_name 姓名,  
  4.        t.sub_name 科目,  
  5.        t.score 成績,  
  6.        rank() over(order by score desc nulls last) 名次  
  7.   from t_score t  
  8.  where t.sub_name = 'Oracle';  

 查詢結果:

學號 姓名 科目 成績 名次
4 楊過     Oracle 77.00 1
2 李四     Oracle 77.00 1
3 張三丰   Oracle 0.00 3

 

3.3查詢Oracle科目成績名次-連續dense_rank

Sql代碼  收藏代碼
  1. --3.查詢Oracle科目成績名次-連續dense_rank  
  2. select t.s_id 學號,  
  3.        t.s_name 姓名,  
  4.        t.sub_name 科目,  
  5.        t.score 成績,  
  6.        dense_rank() over(order by score desc nulls last) 名次  
  7.   from t_score t  
  8.  where t.sub_name = 'Oracle';  

 查詢結果:

 

學號 姓名 科目 成績 名次
4 楊過     Oracle 77.00 1
2 李四     Oracle 77.00 1
3 張三丰   Oracle 0.00 2

 

3.4 查詢各學生各科排名

Sql代碼  收藏代碼
  1. --4.查詢各學生各科排名  
  2. select t.s_id 學號,  
  3.        t.s_name 姓名,  
  4.        t.sub_name 科目,  
  5.        t.score 成績,  
  6.        dense_rank() over(partition by t.s_name order by score desc nulls last) 名次  
  7.   from t_score t;  

 查詢結果:

 

學號 姓名 科目 成績 名次
5 mike     c++ 80.00 1
2 李四     數學 80.00 1
2 李四     Oracle 77.00 2
2 李四     語文 50.00 3
4 楊過     JAVA 90.00 1
4 楊過     Oracle 77.00 2
1 張三     語文 80.00 1
1 張三     數學 0.00 2
3 張三丰   體育 120.00 1
3 張三丰   語文 10.00 2
3 張三丰   Oracle 0.00 3
3 張三丰   數學   4

 

3.5 查詢各科名次(分區)

Sql代碼  收藏代碼
  1. --5.查詢各科名次(分區)  
  2. select t.s_id 學號,  
  3.        t.s_name 姓名,  
  4.        t.sub_name 科目,  
  5.        t.score 成績,  
  6.        dense_rank() over(partition by t.sub_name order by score desc nulls last) 名次  
  7.   from t_score t;  

 查詢結果:

 

學號 姓名 科目 成績 名次
4 楊過     JAVA 90.00 1
4 楊過     Oracle 77.00 1
2 李四     Oracle 77.00 1
3 張三丰   Oracle 0.00 2
5 mike     c++ 80.00 1
2 李四     數學 80.00 1
1 張三     數學 0.00 2
3 張三丰   數學   3
3 張三丰   體育 120.00 1
1 張三     語文 80.00 1
2 李四     語文 50.00 2
3 張三丰   語文 10.00 3

 

來源:http://zhaisx.iteye.com/blog/774165

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章