Windowing function --over()

Source: https://www.cnblogs.com/com-xiaolanchong/p/5796579.html
Comment:

A learning task: each person has different scores, and the highest score of each person is counted.

This problem should still be relatively simple, in fact, just use aggregate functions.

select id,name,max(score) from Student group by id,name order by name

The above case is only applicable if id and name are in one-to-one correspondence, otherwise the queried data will be incorrect.

For example: 1 Zhangsan 100

           2 Zhangsan 90

          The query results of

          two pieces of information will be output.

To avoid this, you can use a windowing function.

Personal understanding is that the function of windowing function and aggregation function are opposite.

Aggregate functions combine multiple rows of data into one row of data; while windowing functions split one row of data into multiple rows.

The windowing function can meet the above problems, and colleagues can also meet other problems. For example: Find the information of the students with the highest grades in each class.

Analysis: Everyone's student number must be different, and the names may have the same name. The most complicated situation is that each class may have more than one highest grade.

        If you continue to use the starting method, it will not meet the requirements.

        Using the window function can solve this problem very well.

--The student with the first grade in each class --The
information in the student table is as follows
a 1 80
b 1 78
c 1 95
d 2 74
e 2 92
f 3 99
g 3 99
h 3 45
i 3 55
j 3 78

The query results are as follows:
c 1 95 1
e 2 92 1
f 3 99 1
g 3 99 1

SQL query statement As follows:
select *
from
( select name,class,s, rank
()over(partition by class order by s desc) mm
from t2

) as t
where t.mm=



1
When the second place is followed by the fourth place
dense_rank(), it is continuously sorted. When there are two second places, it is still followed by the third place

over() windowing function: After using the aggregate function, multiple lines will become one line,
and The windowing function is to turn one row into multiple rows;
and after using the aggregate function, if you want to display other columns, you must add the column to the group by,
and after using the windowing function, you can directly transfer all the information without using the group by display.

Windowing functions apply to the results of adding aggregate functions in the last column of each row.

Commonly used windowing functions:
1. Display aggregate information for each piece of data. (aggregate function() over())
2. Provide grouped aggregate function results for each piece of data (aggregate function() over(partition by field) as alias) --Group by field , and calculate after grouping
3. Use with ranking function (row number() over (order by field) as alias)

common analysis functions: (the most commonly used should be the sorting of 1.2.3)
1. row_number () over(partition by ... order by ...)
2. rank() over(partition by ... order by ...)
3. dense_rank() over(partition by ... order by ... )
4. count() over(partition by ... order by ...)
5. max() over(partition by ... order by ...)
6. min() over(partition by ... order by ...)
7. sum() over(partition by ... order by ...)
8. avg() over(partition by ... order by ...)
9. first_value() over(partition by ...order by ...)
10. last_value() over(partition by ... order by ...)
11. lag() over(partition by ... order by ...)
12. lead() over(partition by ... order by ...)
lag and lead can obtain a certain column of a certain row of several offsets adjacent to the current row in a certain order in the result set (without the self-association of the result set);
lag and lead are forward respectively , backward;
lag and lead have three parameters, the first parameter is the column name, the second parameter is the offset of the offset, and the third parameter is the default value when the recording window is exceeded)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326240977&siteId=291194637