sql: usage of rank/row_number/count/sum/avg/max/min over

Table of contents

Prerequisites: Student table

1、rank()over(..)

2、row_number()over(..)

3、dense_rank()over(..)

4. The aggregation function is used together with over

4.1 sum()over(..)

4.1.1 The first way of writing: sum () over (partition by ..)

4.1.2 The second way of writing: sum() over (order by ..) 

4.1.3 The third way of writing: sum() over ( partition by .. order by .. )

4.1.4 The fourth way of writing: sum() over() 

4.2 count()over(..)

4.2.1 The first way of writing: count() over() 

4.2.2 The second way of writing: count() over(partition by ..) 

4.2.3 The third way of writing: count() over( order by ..) 

4.2.4 The fourth way of writing: count() over( partition by .. order by ..) 

4.3 avg()above(..)

4.3.1 The first way avg() over()

4.3.2 The second way avg() over (partition by ..)

4.3.3 The third way avg() over (order by ..)

4.3.4 The fourth way avg() over (partition by .. order by ..)

4.4 min()over(..)

4.4.1 The first way of writing: min() over() 

4.4.2 The second way of writing: min() over(partition by ..) 

4.4.3 The third way of writing: min() over(order by ..) 

4.4.4 The fourth way of writing: min() over(partition by .. order by ..) 

4.5 max()over(..)

4.5.1 The first way of writing: max() over() 

4.5.2 The second way of writing: max() over(partition by ..) 

4.5.3 The third way of writing: max() over(order by ..) 

4.5.4 The fourth way of writing: max() over(partition by .. order by ..) 

5. Case: Adapted according to RIKO 601. The flow of people in the gymnasium


Prerequisites: Student table

--创建Student表
CREATE TABLE STUDENT(
  "ID" NUMBER(11,0) PRIMARY KEY NOT NULL,
  "STUDENT_NAME" VARCHAR2(255 BYTE),
  "CLASS_NAME" VARCHAR2(255 BYTE),
  "SCORE" NUMBER(18,0)
)

--插入数据
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('1', 'Tom', '1班', '60');
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('2', 'Mary', '2班', '77');
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('3', 'Timir', '1班', '80');
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('4', 'Airo', '1班', '99');
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('5', '小红', '2班', '80');
INSERT INTO "CERPAWCSADM"."STUDENT" VALUES ('6', '小美', '3班', '65');

1、rank()over(..)

Syntax: rank() over (partition by field to be partitioned order by field to be sorted)

Among them, order by must be written, and an error will be reported if it is not written, and partition by can be omitted

1.1 Sorted in ascending order according to score, ranks are sorted serial numbers, starting from 1, increasing in order, if there are two repeated scores, the serial numbers will be the same (score = 80 in the figure below, then ranks = 4), because the two are The serial number of 4, so then jump to the serial number 6

select id,Student_name,class_name,score,rank() over(order by score) ranks from STUDENT

 1.2 Partition according to class_name (equivalent to grouping), and then sort in ascending order according to score, ranks is the sorted serial number, starting from 1, increasing in order, if there is duplicate data, skip the serial number

select id,Student_name,class_name,score,rank() over(partition by class_name order by score) ranks from STUDENT

 2、row_number()over(..)

Syntax: row_number () over (partition by the field to be partitioned order by the field to be sorted)

Among them, order by must be written, and an error will be reported if it is not written, and partition by can be omitted

2.1 Sorting in ascending order according to score, ranks is the sequence number after sorting, starting from 1, increasing in order, regardless of whether there are duplicate scores, the sequence number is increasing, and the sequence number will not be skipped

select id,Student_name,class_name,score,row_number() over(order by score) ranks from STUDENT

 2.2 Partition according to class_name (equivalent to grouping), and then sort in ascending order according to score, ranks is the sorted serial number, starting from 1, increasing in order, without skipping numbers

select id,Student_name,class_name,score,row_number() over(partition by class_name order by score) ranks from STUDENT

  3、dense_rank()over(..)

Syntax: dense_rank() over ( partition by field to be partitioned order by field to be sorted)

Among them, order by must be written, and an error will be reported if it is not written, and partition by can be omitted

3.1 Sorting in ascending order according to score, ranks is the sequence number after sorting, starting from 1, increasing in order, if there are two duplicate scores, the sequence number will be the same (score = 80 in the figure below, then ranks = 4), then the sequence number is 5

select id,Student_name,class_name,score,dense_rank() over(order by score) ranks from STUDENT

4. The aggregation function is used together with over

Syntax: sum() / count() / avg() / min() / max() over ( partition by the field to be partitioned order by the field to be sorted)

Grammatical meaning: sum() over accumulates data according to conditions (detailed in 4.1)

                  count() over Count the number of rows according to the condition  

                  avg() over average based on conditions

                  min() over Find the minimum value according to the condition

                  max() over Find the maximum value according to the condition

Among them, partition by and order by can be written all, or one of them can be written, or none of them can be written

4.1 sum()over(..)

4.1.1 The first way of writing: sum () over (partition by ..)

 First partition (group) according to class_name, then sum the score, and see the result Ranks column

select id,Student_name,class_name,score,sum(score) over(partition by class_name) ranks from STUDENT

4.1.2 The second way of writing: sum() over (order by ..) 

 First sort according to the score, and then accumulate according to the score in turn, see the Ranks column of the accumulation result

select id,Student_name,class_name,score,sum(score) over(order by score) ranks from STUDENT

4.1.3 The third way of writing: sum() over ( partition by .. order by .. )

First partition (group) according to class_name, then sort according to score, and accumulate the scores according to the partition in turn, and see the cumulative result in the Ranks column

select id,Student_name,class_name,score,sum(score) over(partition by class_name order by score) ranks from STUDENT

4.1.4 The fourth way of writing: sum() over() 

 Accumulate all score values, see the accumulation result Ranks column

select id,Student_name,class_name,score,sum(score) over() ranks from STUDENT

4.2 count()over(..)

4.2.1 The first way of writing: count() over() 

count all rows

select id,Student_name,class_name,score,count(*) over() studentCount from STUDENT

4.2.2 The second way of writing: count() over(partition by ..) 

 Count the number of students according to the class_name partition

select id,Student_name,class_name,score,count(student_name) over(partition by class_name) studentCount from STUDENT

4.2.3 The third way of writing: count() over( order by ..) 

According to the score sorting, the number of rows is accumulated in turn. Note that if the scores are the same, then the same studentCount is counted as: the number counted last time (3) + the number of the same score (2) = 5, and the next step is to sort in order for 6

4.2.4 The fourth way of writing: count() over( partition by .. order by ..) 

Partition according to class_name, then sort according to score, and accumulate the number of rows in turn

select id,Student_name,class_name,score,count(student_name) over(partition by class_name order by score) studentCount from STUDENT

4.3 avg()above(..)

4.3.1 The first way avg() over()

Find the average of all scores

select id,Student_name,class_name,score,AVG(score) over() ranks from STUDENT

4.3.2 The second way avg() over (partition by ..)

According to the class_name partition, find the average score of each class

select id,Student_name,class_name,score,AVG(score) over(partition by class_name) ranks from STUDENT

4.3.3 The third way avg() over (order by ..)

Sorting according to score, accumulatively calculating the average value of score

select id,Student_name,class_name,score,AVG(score) over(order by score) ranks from STUDENT

4.3.4 The fourth way avg() over (partition by .. order by ..)

Partition according to class_name and then sort according to score, accumulatively calculate the average value of score (in each partition)

select id,Student_name,class_name,score,AVG(score) over(partition by class_name order by score) ranks from STUDENT

4.4 min()over(..)

4.4.1 The first way of writing: min() over() 

Find the lowest score of all students' scores

select id,student_name,class_name,score,min(score) over() minScore from STUDENT

4.4.2 The second way of writing: min() over(partition by ..) 

According to the class_name partition, find the lowest score of each class student score

select id,student_name,class_name,score,min(score) over(partition by class_name ) minScore from STUDENT

4.4.3 The third way of writing: min() over(order by ..) 

Sort by score, find the lowest score of all students

Note: min over (order by score) is an ascending order to find the minimum value, and it has no effect on (order by score desc) a descending order

select id,student_name,class_name,score,min(score) over(order by score ) minScore from STUDENT

4.4.4 The fourth way of writing: min() over(partition by .. order by ..) 

Partition according to class_name, and then sort according to score, find the lowest score of each class student score

Note: Use order by score to sort in ascending order to find the minimum value, and order by score desc to sort in descending order has no effect

select id,student_name,class_name,score,min(score) over(partition by class_name order by score) minScore from STUDENT

4.5 max()over(..)

4.5.1 The first way of writing: max() over() 

Find the highest score of all students' grades

select id,student_name,class_name,score,max(score) over() maxScore from STUDENT

4.5.2 The second way of writing: max() over(partition by ..) 

According to the class_name partition, find the highest score of each class student score

select id,student_name,class_name,score,max(score) over(partition by class_name) maxScore from STUDENT

4.5.3 The third way of writing: max() over(order by ..) 

Sort by score, find the highest score of all students

Note: max over (order by score desc) is a descending sort to find the maximum value, and it has no effect on (order by score) an ascending sort

select id,student_name,class_name,score,max(score) over(order by score desc) maxScore from STUDENT

4.5.4 The fourth way of writing: max() over(partition by .. order by ..) 

Partition according to class_name, and then sort according to score, find the highest score of each class student score

Note: Use order by score desc to sort in descending order to find the maximum value, and it has no effect on order by score to sort in ascending order

select id,student_name,class_name,score,max(score) over(partition by class_name order by score desc) maxScore from STUDENT

5. Case: Adapted according to 601. The flow of people in the gymnasium

id Write a SQL query to query three or more consecutive rows of records where the student's score is greater than or equal to 80  , and the returned results are sorted in ascending order of class names

The first way to write a subquery: 

select b.id,b.Student_name,b.class_name,b.score from 
(select a.id,a.Student_name,a.class_name,a.score,count(*) over(partition by a.a1) a2 from 
    -- a2 表示根据a1分区统计数量
		(
		select id,Student_name,class_name,score,id - row_number() over(order by id) a1 
		-- a1 表示的是id 是否连续,也就是id - row_number()的值是否相同
		from STUDENT where score >= 80  -- 分数大于或等于 80
		) a
)b
where b.a2>=3 order by b.class_name --找出结果大于3条的并且根据班名升序排序

The second way to write a subquery: 

with a as
(
	select id,Student_name,class_name,score,id - row_number() over(order by id) a1 
	from STUDENT where score >= 80
),
b as 
(
    select id,Student_name,class_name,score,count(*) over(partition by a1) a2 from a
)
select  id,Student_name,class_name,score from b where  b.a2>=3 order by b.class_name

Guess you like

Origin blog.csdn.net/SUMMERENT/article/details/129498766