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 uses openlayers to load Tiandi Map and Amap

Table of contents 1. World Map 1. Install openlay...

HTML Marquee character fragment scrolling

The following are its properties: direction Set th...

A brief analysis of adding listener events when value changes in html input

The effect to be achieved In many cases, we will ...

Implementation of Vue package size optimization (from 1.72M to 94K)

1. Background I recently made a website, uidea, w...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

Use of Linux passwd command

1. Command Introduction The passwd command is use...

A small introduction to the use of position in HTML

I just learned some html yesterday, and I couldn&#...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

Analysis of the Principle of MySQL Index Length Limit

This article mainly introduces the analysis of th...

Detailed explanation of Mysql communication protocol

1.Mysql connection method To understand the MySQL...

Solve the margin: top collapse problem in CCS

The HTML structure is as follows: The CCS structu...

Summary of common optimization operations of MySQL database (experience sharing)

Preface For a data-centric application, the quali...

Create an SSL certificate that can be used in nginx and IIS

Table of contents Creating an SSL Certificate 1. ...