Knowledge of window functions

Knowledge of window functions:

1. The position of <window function> can be placed in the following two functions:
1) Special window functions, including rank, dense_rank, row_number and other special window functions to be mentioned later.
2) Aggregate functions, such as sum. avg, count, max, min, etc.

2. Usage
Because window functions operate on the results of where or group by clauses, in principle , window functions can only be written in select clauses .
Simply put, the window function has the following functions:
1) It has the functions of grouping and sorting at the same time
2) Does not reduce the number of rows in the original table
3) The syntax is as follows:

<窗口函数> over (partition by <用于分组的列名>
                order by <用于排序的列名>)

Third, the special features of the
window function Window function has the function of group by clause grouping and order by clause sorting function that we have learned before. So, why use window functions?
This is because group by changes the number of rows in the table after grouping and summarizing, and there is only one category in a row. The partition by and rank functions will not reduce the number of rows in the original table . For example, the following counts the number of people in each class.
Insert picture description here
That is, the window function has the following functions:

1) It has both partition by and order by functions.
2) Does not reduce the number of rows in the original table, so it is often used to rank within each group

4. An example of a special window function:
Insert picture description here
Take class "1" as an example. The grade "95" of this class is ranked first, and the class "83" is ranked fourth. The above result is indeed ranked according to our requirements in each class.

select *,
   rank() over (partition by 班级
                 order by 成绩 desc) as ranking
from 班级表

Let's explain the select clause in this sql statement. Rank is a sorting function. The requirement is "rank by grade within each class". This sentence can be divided into two parts:

1) Within each class:
partition by class is used to group tables. In this example, so we specify the "class" grouping (partition by class)
2) Rank by grade
The function of the order by clause is to sort the grouped results, the default is to sort the results in ascending order (asc). In this example (order by grade desc) is sorted by the column of grades, and the desc keyword is added to indicate descending order.
Through the following figure, we can understand the role of partition by (grouping) and order by (sorting within a group).
Insert picture description here
The partition clause is omitted, and the omission means that the grouping is not specified. The results are as follows, but the results are sorted from high to low:

select *,
   rank() over (order by 成绩 desc) as ranking,
   dense_rank() over (order by 成绩 desc) as dese_rank,
   row_number() over (order by 成绩 desc) as row_num
from 班级表

Insert picture description here
Insert picture description here
Insert picture description here
5. Aggregate function as
a window function. The usage of the aggregation and window functions is exactly the same as the dedicated window function mentioned above. You only need to write the aggregation function in the position of the window function, but the parentheses after the function cannot be empty , you need to specify the aggregation Column name.

select *,
   sum(成绩) over (order by 学号) as current_sum,
   avg(成绩) over (order by 学号) as current_avg,
   count(成绩) over (order by 学号) as current_count,
   max(成绩) over (order by 学号) as current_max,
   min(成绩) over (order by 学号) as current_min
from 班级表

Insert picture description here
Aggregate function as a window function, you can intuitively see in the data of each row, as of the data in this row, what is the statistical data (maximum, minimum, etc.). At the same time, you can see the impact of each row of data on the overall statistics.

Six. Window function usage scenarios
1) Business requirements "rank in each group", such as:
ranking problem: each department ranks
topN according to performance problem: find the top N employees in each department for reward

Guess you like

Origin blog.csdn.net/weixin_46801232/article/details/108669820