MySQL basics (6)-MySQL window function

Preface

  The last article introduced you to some commonly used functions in MySQL , which mainly include mathematical functions, string functions, date and time functions, grouping and merging functions, logical functions, and in fact there are windowing functions, but the windowing function is a new version of MySQL. So, we will introduce it in detail in a separate article. First, we introduce the theoretical knowledge of the window function.

1. Relevant theoretical knowledge of window function

  In fact, the windowing function is a new function added after MySQL8.0. Therefore, if you want to use the windowing function directly, the mysql version must be above 8.0. In fact, the windowing function is a special function executed on the record set that meets certain conditions. For each record, a function must be executed in this window. Some functions have a fixed window size depending on the record. This is a static window; some functions are the opposite. Different records correspond to different windows. A dynamically changing window is called a sliding window. The essence of windowing function is still aggregation operation, but it is more flexible. It calculates each row of data using the row related to that row and returns the calculation result. Next, we introduce the relevant syntax of the window function; the specific syntax is as follows:

Window function name ([<field name>]) over([partition by <grouping field>] [order by <sorting field> [desc]] [<subdivision window>])

  A concept of the windowing function is the current line, the current line belongs to a certain window, the window is specified by the over keyword to specify the window range of the function execution, if nothing is written in the following brackets, it means that the window contains all lines that meet the where condition , The window function is calculated based on all rows; if it is not empty, there are three parameters to set the window, the meaning of the specific parameters is as follows:

  • Partition by clause : partition according to the specified field, the two partitions are separated by the boundary, the windowing function is executed in different partitions, and it is reinitialized when the partition boundary is crossed.
  • Order by clause : Sort according to the specified field, and the window function will number the records according to the sorted order. It can be used in conjunction with the partition by clause, or it can be used alone.
  • Frame clause : A subset of the current partition, used to define the rules of the subset, usually used as a sliding window.

  Sliding window for the specified range, typically using between frame_start and frame_endthe syntax to indicate the scope of the line, frame_start和frame_endwe can support the following keywords to determine different dynamic rows:

  • The current row boundary is the current row, generally used with other range keywords
  • The unbounded preceding boundary is the first row in the partition
  • The unbounded following boundary is the last row in the partition
  • expr preceding boundary is the current line minus the value of expr
  • The expr following boundary is the current line plus the value of expr

  For example, the following are all legal ranges:

  • The rows between 1 preceding and 1 following window range is the current row, the previous row, and the next row, a total of three rows of records.
  • The rows unbounded preceding window range is from the current row to the last row in the partition.
  • rows between unbounded preceding and unbounded following window range is all rows in the current partition, which is equivalent to not writing

  In fact, the range of these sliding windows is shown as follows:

  In fact, it is not difficult to find that the windowing function is essentially a normal function, but it is still different from our general aggregate function:

  • The aggregation function aggregates multiple records into one; while the window function is executed for each record, whether there are a few records after the execution or a few.
  • Aggregate functions can also be used in windowing functions.

  Next, we continue to introduce the serial number function. It is also a window function.
1. row_number()Display the non-repeated and uninterrupted serial number in the partition.
2. dense_rank()Display the repeated and uninterrupted serial number in the partition.
3. rank()Display the repeated and intermittent serial number in the partition.

Two, window function case

  As we said at the beginning of the article, to use the windowing function, our local MySQL version must be 8.0 or above. Originally, readers who saw this should all check their own version, but in order to ensure that readers can practice the next case, let's first introduce the MySQL version query and download URL. When we check our MySQL version, first open the black screen terminal, and use the 菜单键+Rshortcut key to open the black screen terminal. Then enter the following command:

mysql -uroot -p

  Then enter the password to see your version, as follows:

  If your mysql is another version, it is recommended to install it after uninstallation. There are many articles about uninstallation. You can search for it and then install it directly . Since there are many articles in this area, I won't introduce them here. After installation, you can directly practice the windowing function. What needs to be explained here is: the database and data we use are mentioned in the previous article . If you practice actual combat, you can go to the previous article to create a database and data table.

  • 1. Calculate the average salary of all employees .
-- 我们先用普通函数进行实现:
select avg(sal) 
from emp;

  The results of the implementation are as follows:

-- 当然,我们也可以用开窗函数实现
select *,avg(sal) over() avg_sal 
from emp;

  The results of the execution are as follows:

  As can be seen from the results: our window function displays each row, while the ordinary aggregate function has only one result.

  • 2. Query each department to calculate the total cumulative salary according to the entry date
select deptno, avg(sal) avg_sal 
from emp 
group by deptno;

  The results of the implementation are as follows:

select *,avg(sal) over(partition by deptno) avg_sal 
from emp;

  The results of the execution are as follows:

  As can be seen from the results: our window function displays each line and outputs it according to the group. The average salary of each line in the group is the same; while our general aggregate function outputs by department, each Department output is one line. Then the readers who see here can't help but have a question: Since the windowing function can be implemented by our general aggregate function, why is there a windowing function? In fact, when we introduced the theory earlier, we also introduced the difference between aggregate functions and windowing functions. In the following case, it can only be realized with a windowing function, other functions cannot be realized;

  • 3. Each department calculates the sum of accumulated wages based on the entry date
select *, sum(sal) over(partition by deptno  order by hiredate) sum_sal 
from emp;

  The results of the implementation are as follows:

  We can see the uniqueness of our windowing function from this topic. Next, we are looking at a case;

  • 4. Each department calculates the average salary of the previous line and the next line of the current line according to the entry date
select *,avg(sal) over(partition by deptno order by hiredate rows between 1 preceding and 1 following) avg_sal 
from emp;

  The results of the execution are as follows:

  Generally speaking, the serial number function is generally used together with the window function, and the effect is very good, just to solve the TOPN problem;

  • 5. Query the ranking of all employees according to their entry date
select *, row_number() over(order by hiredate) 排名 
from emp;

  The results of the implementation are as follows:

  • 6. The employees of each department are ranked according to their basic salary.

  Here we first display according to normal thinking:

select *, row_number() over(partititon by deptno order by sal desc) 排名
from emp;

  The results of the implementation are as follows:

  However, there is a problem here: the basic salary is the same. It stands to reason that the ranking should be the same. The specific implementation is as follows:

*select *,
		row_number() over(partition by deptno order by sal desc) 排名1,
        dense_rank() over(partition by deptno order by sal desc) 排名2,
        rank() over(partition by deptno order by sal desc) 排名3
from emp;

  The results of the implementation are as follows:

to sum up

  The last article introduced you to some commonly used functions in MySQL , which mainly include mathematical functions, string functions, date and time functions, grouping and merging functions, logical functions, and in fact there are windowing functions, but the windowing function is a new version of MySQL. Therefore, we will introduce it in detail in a separate article. Mainly include window function and serial number function. In addition, some cases are used to further explain its usage and function. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/115276488