Mysql8.0 uses window functions to solve sorting problems

Mysql8.0 uses window functions to solve sorting problems

Introduction to MySQL Window Functions

MySQL has supported window functions since MySQL 8.0. This feature has long been supported in most commercial databases and some open source databases, and some are also called analytical functions.

What is a window?

The concept of window is very important. It can be understood as a set of records. Window function is a special function executed on a set of records that meet certain conditions. For each record, a function must be executed within this window. For some functions, the window size is fixed depending on the record, which is a static window. For some functions, on the contrary, different records correspond to different windows. This dynamically changing window is called a sliding window.

Window functions and ordinary aggregate functions are also easily confused. The differences between the two are as follows:

Aggregate functions aggregate multiple records into one; whereas window functions are executed on each record, regardless of the number of records that need to be executed.

Aggregate functions can also be used in window functions, which will be illustrated with examples later.

1. MySQL 5.0

For example: we want to calculate the sales of sales personnel, the results should be sorted from high to low, and the query results should include the sales ranking.


This is a part of the data from a product order table.

1. Calculate the sales of sales personnel and sort the results from high to low

In this part, we can directly use group by to group the sales staff, use the aggregate function sum to sum the sales, and then use order by to sort the sales results. The statement is as follows:

SELECT sales_name, sum( profit ) FROM spm_order GROUP BY sales_name ORDER BY sum( profit ) DESC 


Query results

2. If the query results are to include sales rankings.

In MySQL 5.0, we need to define a sorting auto-increment variable so that it can automatically add +1 as a new column. The statement is as follows:

SET @rank = 0;
SELECT
A.*,
@rank := @rank + 1 AS rank_no
FROM
( SELECT sales_name, sum( profit ) FROM spm_order GROUP BY sales_name ORDER BY sum( profit ) DESC ) A

The := here means assignment, and the A here is to give the subquery an alias for easy calling.

The results are as follows:


insert image description here

2. MySQL 8.0

For this problem, there are special window functions that can be called in MySQL 8.0 to simplify complex problems.

The statement is as follows:

SELECT
sales_name,
sum( sales ),
row_number ( ) over ( ORDER BY sum( sales ) DESC ) AS 'rank'
FROM
spm_order
GROUP BY
sales_name

result:


insert image description here

Here we use [ row_number() over () ] and directly write the content we want to sort in over().
Most companies at work use version 5.0, so learn more about it.

Summarize

The above is what I introduced to you about using window functions in Mysql8.0 to solve sorting problems. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • MySQL 8.0 Window Function Introduction and Summary
  • Case analysis of SQL window functions to achieve efficient paging query
  • Specific use of MySQL window functions
  • Quickly learn about SQL window functions

<<:  Solution to the "No such file or directory" prompt when executing executable files in Linux

>>:  JavaScript offsetParent case study

Recommend

Vue implements page caching function

This article example shares the specific code of ...

Summary of how to use bootstrap Table

This article shares with you how to use bootstrap...

How to install docker on Linux system and log in to docker container through ssh

Note: I use Centos to install docker Step 1: Inst...

In-depth analysis of MySQL data type DECIMAL

Preface: When we need to store decimals and have ...

Understanding of web design layout

<br />A contradiction arises. In small works...

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

HTML Grammar Encyclopedia_HTML Language Grammar Encyclopedia (Must Read)

Volume Label, Property Name, Description 002 <...

html page!--[if IE]...![endif]--Detailed introduction to usage

Copy code The code is as follows: <!--[if IE]&...

Vue project code splitting solution

Table of contents background Purpose Before split...

Detailed steps to install MySQL 5.6 X64 version under Linux

environment: 1. CentOS6.5 X64 2.mysql-5.6.34-linu...

mysql5.7.18.zip Installation-free version configuration tutorial (windows)

This is the installation tutorial of mysql5.7.18....

Use of MySQL triggers

Triggers can cause other SQL code to run before o...

Examples of correct judgment methods for data types in JS

Table of contents Preface Can typeof correctly de...

Detailed explanation of the usage of grep command in Linux

1. Official Introduction grep is a commonly used ...