rank oracle window function of the ranking function

- known: two kinds are ranked ( partition and not partition): with and without partition

- two calculations ( continuous, discontinuous), the corresponding function: dense_rank, rank

 

· Raw data query: Student ID, name, account name, score

select * from t_score

S_ID

S_NAME

SUB_NAME

SCORE

1

Joe Smith                  

Chinese                             

80.00                 

2

John Doe   

mathematics

80.00

1

Joe Smith   

mathematics

0.00

2

John Doe   

Chinese

50.00

3

Chi Master 

Chinese

10.00

3

Chi Master 

mathematics

 

3

Chi Master 

physical education

120.00

4

Yang Guo   

Java

90.00

5

mike   

c++

80.00

3

Chi Master 

Oracle

0.00

4

Yang Guo   

oracle

77.00

2

John Doe   

Oracle

77.00

 

· Oracle query subjects for each student ranking (simple ranking)

select sc.s_id,sc.s_name,sub_name,sc.score,

 rank() over (order by score desc名次

from t_score sc

where sub_name='Oracle'

 

S_ID

S_NAME

SUB_NAME

SCORE

Ranking

4             

Yang Guo                        

Oracle            

77.00                    

1                     

2

John Doe   

Oracle

77.00

1

3

Chi Master 

Oracle

0.00

3

 

 

Contrast: rank () and dense_rank (): discontinuous and continuous Rank Rank (it is simple ranking)

 

select sc.s_id,sc.s_name,sub_name,sc.score,

 dense_rank() over (order by score desc名次

from t_score sc

where sub_name='Oracle'

S_ID

S_NAME

SUB_NAME

SCORE

Ranking

4                      

Yang Guo                 

Oracle

77.00                  

1                      

2

John Doe   

Oracle

77.00

1

3

Chi Master 

Oracle

0.00

2

 

• The query subjects each student ranking ( partition rank)

select sc.s_id,sc.s_name,sub_name,sc.score,

 rank() over

 (partition by sub_name order by score desc名次

from t_score sc

S_ID

S_NAME

SUB_NAME

SCORE

Ranking

4                  

Yang Guo                      

JAVA                       

90.00                  

1                       

4

Yang Guo   

Oracle

77.00

1

2

John Doe   

Oracle

77.00

1

3

Chi Master 

Oracle

0.00

3

5

mike   

c++

80.00

1

3

Chi Master 

mathematics

 

1

2

John Doe   

mathematics

80.00

2

1

Joe Smith   

mathematics

0.00

3

3

Chi Master 

physical education

120.00

1

1

Joe Smith   

Chinese

80.00

1

2

John Doe   

Chinese

50.00

2

3

Chi Master 

Chinese

10.00

3

 

• The query subjects before 2 name (for partition rank)

· Similar: News table, find the column hits the previous three news.

Product table, find the top 10 of each category of merchandise sales.

select * from (

select sc.s_id,sc.s_name,sub_name,sc.score,

dense_rank() over

(partition by sub_name order by score desc) 名次

from t_score sc

) x

where x. Place <= 2

S_ID

S_NAME

SUB_NAME

                SCORE

   Ranking

4                     

Yang Guo                        

JAVA                    

                   90.00 

       1                            

4

Yang Guo   

Oracle

77.00

       1

2

John Doe   

Oracle

77.00

       1

3

Chi Master 

Oracle

0.00

       2

5

mike   

c++

80.00

       1

3

Chi Master 

mathematics

 

       1

2

John Doe   

mathematics

80.00

       2

3

Chi Master 

physical education

120.00

       1

1

Joe Smith   

Chinese

80.00 

       1 

2

John Doe   

Chinese

50.00

       2

 

 

• The query each student scores

select s_id,s_name,sum(score) sum_score from t_score

group by s_id,s_name

S_ID

S_NAME

SUM_SCORE

1                       

Joe Smith                         

80.00                                 

2

John Doe   

207.00

3

张三丰 

130.00

4

杨过   

167.00

5

mike   

80.00

 

·根据总分查询各同学名次

select x.*,

rank() over (order by sum_score desc名次

from (

select s_id,s_name,sum(score) sum_score from t_score

group by s_id,s_name ) x

S_ID

S_NAME

SUM_SCORE

名次

2                   

李四                       

207.00                       

1

4

杨过   

167.00

2

3

张三丰 

130.00

3

1

张三   

80.00

4

5

mike   

80.00

4

 

 

语法:

rank() over (order by 排序字段 顺序)

rank() over (partition by 分组字段 order by 排序字段 顺序)

 

1.顺序:asc|desc  名次与业务相关:

  示例:找求优秀学员:成绩:降序  迟到次数:升序

2.分区字段:根据什么字段进行分区。

 

问题:分区与分组有什么区别?

·分区只是将原始数据进行名次排列(记录数不变),

·分组是对原始数据进行聚合统计(记录数变少,每组返回一条),注意:聚合。

 

脚本:

create table t_score

(

  autoid   number  primary key,

  s_id     number(3),

  s_name   char(8) not null,

  sub_name varchar2(20),

  score    number(10,2)

);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (8, 1, '张三    ', '语文', 80);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (9, 2, '李四    ', '数学', 80);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (10, 1, '张三    ', '数学', 0);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (11, 2, '李四    ', '语文', 50);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (12, 3, '张三丰  ', '语文', 10);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (13, 3, '张三丰  ', '数学', null);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (14, 3, '张三丰  ', '体育', 120);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (15, 4, '杨过    ', 'java', 90);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (16, 5, 'mike    ', 'c++', 80);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values (3, 3, '张三丰  ', 'oracle', 0);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values ​​(4, 4, 'Yang Guo', 'oracle', 77);

insert into t_score (autoid, s_id, s_name, sub_name, score)

values ​​(17, 2, 'John Doe', 'oracle', 77);

commit;

 


Guess you like

Origin blog.csdn.net/l3922768721/article/details/77528497