New features in MySQL 8.0 include:
1. Problem MySQL 8.0.2 adds a new major feature - window function What specific problem does this feature solve? Let's first look at a SQL query scenario to see how we usually do it, and then see how to use window functions to solve it more conveniently. (1) Prepare test tables and data Create a simple movie information table with the following fields: ID release_year category_id (category ID) rating CREATE TABLE films ( id int(11), release_year int(11), category_id int(11), rating decimal(3,2) ) Insert test data insert into films2 values (1,2015,1,8.00), (2,2015,2,8.50), (3,2015,3,9.00), (4,2016,2,8.20), (5,2016,1,8.40), (6,2017,2,7.00); The overall form is as follows (2) Query requirements Query the average score for each year, and require that the average score for that year be displayed after each record For example, in 2015, there are 3 records with scores of 8.00, 8.50, and 9.00, and the average score is 8.5. In 2016, there are 2 records with an average score of 8.3. In 2017, there is 1 record with an average score of 7.00. The final result is of the following form: We can use a subquery to calculate the average score for each year, and then use join to connect the results back together. SELECT f.id, f.release_year, f.rating, years.year_avg FROM films f LEFT JOIN ( SELECT f.release_year, AVG(rating) AS year_avg FROM films f GROUP BY f.release_year ) years ON f.release_year = years.release_year Is it a bit complicated? Let's take a look at how window functions are handled. 2. Solution with window functions What are window functions Window functions are used to calculate a set of data. Unlike group by, they do not output a single row of results, but are associated with each record. Syntax example: SELECT function_name OVER ( window_definition ) FROM (...) Window_definition is the set of records to be calculated, like a small window that displays a part of the overall data set. function_name specifies what calculation to perform on the data set in the window Looking back at the query above, we need to calculate the average rating of all movies in each year. We use a window function to handle this. SELECT f.id, f.release_year, f.category_id, f.rating, AVG(rating) OVER (PARTITION BY release_year) AS year_avg FROM films f The window_definition part uses the PARTITION BY clause, which tells the database to split the resulting data set into smaller parts, putting the same release_year together. The function AVG(rating) will be calculated for each window data and then put the result into each row. Query Example 1 Calculate the rating ranking position of each movie in its year Query Statement SELECT f.id, f.release_year, f.category_id, f.rating, RANK() OVER (PARTITION BY release_year ORDER BY rating DESC) AS year_rank FROM films f The window_definition part uses PARTITION BY to partition the window by release_year and uses ORDER BY to sort within the window. The RANK() function returns the position of a row of data in this window. Query results Query Example 2 See where each movie ranks in the overall rankings Query Statement SELECT f.id, f.release_year, f.category_id, f.rating, RANK() OVER (ORDER BY rating DESC) AS general_rank FROM films f order by id The order by clause in the main statement ensures that the entire data set is sorted. If PARTITION BY is not used in window_definition, the entire result set is treated as a window, and ORDER BY sorts the data in the window in descending order according to rating, putting the highest-scoring data at the front. The RANK() function obtains the position of each record in the window Query results 3. Summary Window functions are an advanced feature in MySQL 8.0.2 that can easily perform aggregate calculations without actually aggregating the result set, greatly increasing flexibility, readability, and making maintenance easier. You may also be interested in:
|
<<: jQuery simulates picker to achieve sliding selection effect
>>: Centos7.3 How to install and deploy Nginx and configure https
It is too troublesome to find the installation tu...
Table of contents Results at a Glance Heart Effec...
In Beginners' Understanding MySQL Deadlock Pr...
In daily development, front-end students often ar...
This article shares with you how to use canvas an...
This article shares the installation and configur...
1. Document flow and floating 1. What is document...
On Saturday, the redis server on the production s...
Scenario 1: Html: <div class="outer"...
First, download the installation package from the...
Introduction to Linux top command The top command...
Table of contents 1. Detailed explanation of MySQ...
When you feel that there is a problem with MySQL ...
Any number of statements can be encapsulated thro...
Installation path: /application/mysql-5.5.56 1. P...