当前位置:网站首页>SQL language (V)
SQL language (V)
2022-07-25 11:31:00 【Small black board】
The first 5 Chapter :SQL Advanced processing
Reference for this study Datawhale Open source learning wonderful-sql
For the whole content of this article, please refer to the above links , In addition, each statement will be implemented and the results will be displayed , At the same time, give the solution to the problem .
5.1 Window function
5.1.1 The concept of window function and its basic usage
The window function is also called OLAP function .OLAP yes OnLine AnalyticalProcessing For short , It means real-time analysis and processing of database data .
For the sake of understanding , be called Window function . The conventional SELECT Statements are used to query the whole table , and The window function allows us to selectively summarize some data 、 Calculate and sort .
General form of window function :
< Window function > OVER ([PARTITION BY < Name >]
ORDER BY < Column names for sorting >)
[ ] Can be omitted .
The key of window function is to understand the keyword PARTITON BY and ORDER BY The role of .
PARTITON BY Is used to group , That is, select which window you want to see , Be similar to GROUP BY Clause grouping function , however PARTITION BY Clause does not have GROUP BY The summary function of clause , It does not change the number of rows recorded in the original table .
ORDER BY It's used to sort , That is, in the decision window , According to that rule ( Field ) To sort .
Take a chestnut :
mysql> SELECT product_name
,product_type
,sale_price
,RANK() OVER (PARTITION BY product_type
ORDER BY sale_price) AS ranking
FROM product;
+--------------+--------------+------------+---------+
| product_name | product_type | sale_price | ranking |
+--------------+--------------+------------+---------+
| Ball pen | Office Supplies | 100 | 1 |
| Punch | Office Supplies | 500 | 2 |
| Fork | Kitchenware | 500 | 1 |
| Clean the board | Kitchenware | 880 | 2 |
| kitchen knife | Kitchenware | 3000 | 3 |
| pressure cooker | Kitchenware | 6800 | 4 |
| T T-shirt | clothes | 1000 | 1 |
| motion T T-shirt | clothes | 4000 | 2 |
+--------------+--------------+------------+---------+
8 rows in set (0.04 sec)
PARTITION BY Can set the window object range . In this case , In order to sort by commodity category , We have designated product_type. That is, a commodity category is a small " window ".
ORDER BY Can specify which column to follow 、 In what order . In order to arrange in ascending order of sales unit price , We have designated sale_price. Besides , In the window function ORDER BY And SELECT At the end of the statement ORDER BY equally , You can use keywords ASC/DESC To specify ascending order / Descending . If the keyword is omitted, it will follow by default ASC, That is, sort in ascending order .
5.2 Window function types
For the most part , Window functions can be divided into two categories .
One is take SUM、MAX、MIN etc. Aggregate functions Used in window functions
Two is RANK、DENSE_RANK etc. Sort Special window functions used
5.2.1 Special window functions
- RANK function
When calculating the sort , If there are records of the same order , Will skip the next position .
example ) Yes 3 Record number one 1 When a :1 position 、1 position 、1 position 、4 position ……
- DENSE_RANK function
It's also sort by calculation , Even if there are records of the same rank , And I won't skip the next place .
example ) Yes 3 Record number one 1 When a :1 position 、1 position 、1 position 、2 position ……
- ROW_NUMBER function
Give a unique continuous position .
example ) Yes 3 Record number one 1 When a :1 position 、2 position 、3 position 、4 position
Run the following code :
mysql> SELECT product_name
,product_type
,sale_price
,RANK() OVER (ORDER BY sale_price) AS ranking
,DENSE_RANK() OVER (ORDER BY sale_price) AS dense_ranking
,ROW_NUMBER() OVER (ORDER BY sale_price) AS row_num
FROM product;
+--------------+--------------+------------+---------+---------------+---------+
| product_name | product_type | sale_price | ranking | dense_ranking | row_num |
+--------------+--------------+------------+---------+---------------+---------+
| Ball pen | Office Supplies | 100 | 1 | 1 | 1 |
| Punch | Office Supplies | 500 | 2 | 2 | 2 |
| Fork | Kitchenware | 500 | 2 | 2 | 3 |
| Clean the board | Kitchenware | 880 | 4 | 3 | 4 |
| T T-shirt | clothes | 1000 | 5 | 4 | 5 |
| kitchen knife | Kitchenware | 3000 | 6 | 5 | 6 |
| motion T T-shirt | clothes | 4000 | 7 | 6 | 7 |
| pressure cooker | Kitchenware | 6800 | 8 | 7 | 8 |
+--------------+--------------+------------+---------+---------------+---------+
8 rows in set (0.03 sec)
5.2.2 The use of aggregate functions on window functions
The use of aggregate function in windowing function is the same as the previous special window function , But the result is Cumulative Aggregate function value of .
Run the following code :
mysql> SELECT product_id
,product_name
,sale_price
,SUM(sale_price) OVER (ORDER BY product_id) AS current_sum
,AVG(sale_price) OVER (ORDER BY product_id) AS current_avg
FROM product;
+------------+--------------+------------+-------------+-------------+
| product_id | product_name | sale_price | current_sum | current_avg |
+------------+--------------+------------+-------------+-------------+
| 0001 | T T-shirt | 1000 | 1000 | 1000.0000 |
| 0002 | Punch | 500 | 1500 | 750.0000 |
| 0003 | motion T T-shirt | 4000 | 5500 | 1833.3333 |
| 0004 | kitchen knife | 3000 | 8500 | 2125.0000 |
| 0005 | pressure cooker | 6800 | 15300 | 3060.0000 |
| 0006 | Fork | 500 | 15800 | 2633.3333 |
| 0007 | Clean the board | 880 | 16680 | 2382.8571 |
| 0008 | Ball pen | 100 | 16780 | 2097.5000 |
+------------+--------------+------------+-------------+-------------+
8 rows in set (0.08 sec)
It can be seen that , The result of the aggregate function is , Sort as we specify , Here is product_id, Current line and all previous lines The sum or mean of . That is, the aggregate accumulated to the current line .
5.3 Application of window function - Calculate the moving average
Mentioned above , Aggregate function when used in window function , It calculates the aggregation of all data accumulated to the current row . actually , You can also specify more detailed Summary scope . The summary range becomes frame (frame).
grammar
< Window function > OVER (ORDER BY < Column names for sorting >
ROWS n PRECEDING )
< Window function > OVER (ORDER BY < Column names for sorting >
ROWS BETWEEN n PRECEDING AND n FOLLOWING)
PRECEDING(“ Before ”), Specify the frame as “ By the end of n That's ok ”, Add your own line
FOLLOWING(“ after ”), Specify the frame as “ Until after n That's ok ”, Add your own line
BETWEEN 1 PRECEDING AND 1 FOLLOWING, Specify the frame as “ Before 1 That's ok ” + “ after 1 That's ok ” + “ Oneself ”
Execute the following code :
mysql> SELECT product_id
,product_name
,sale_price
,AVG(sale_price) OVER (ORDER BY product_id
ROWS 2 PRECEDING) AS moving_avg
,AVG(sale_price) OVER (ORDER BY product_id
ROWS BETWEEN 1 PRECEDING
AND 1 FOLLOWING) AS moving_avg
FROM product;
+------------+--------------+------------+------------+------------+
| product_id | product_name | sale_price | moving_avg | moving_avg |
+------------+--------------+------------+------------+------------+
| 0001 | T T-shirt | 1000 | 1000.0000 | 750.0000 |
| 0002 | Punch | 500 | 750.0000 | 1833.3333 |
| 0003 | motion T T-shirt | 4000 | 1833.3333 | 2500.0000 |
| 0004 | kitchen knife | 3000 | 2500.0000 | 4600.0000 |
| 0005 | pressure cooker | 6800 | 4600.0000 | 3433.3333 |
| 0006 | Fork | 500 | 3433.3333 | 2726.6667 |
| 0007 | Clean the board | 880 | 2726.6667 | 493.3333 |
| 0008 | Ball pen | 100 | 493.3333 | 490.0000 |
+------------+--------------+------------+------------+------------+
8 rows in set (0.04 sec)
5.3.1 Application scope and precautions of window function
- In principle, , Window functions can only be used in SELECT Used in clauses .
- Window function OVER Medium ORDER BY Clause does not affect the sorting of the final results . It is only used to determine the order in which window functions are evaluated .
5.4 GROUPING Operator
5.4.1 ROLLUP - Calculate total and subtotal
The conventional GROUP BY You can only get the subtotal of each category , Sometimes you need to calculate the total of categories , It can be used ROLLUP keyword .
mysql> SELECT product_type
,regist_date
,SUM(sale_price) AS sum_price
FROM product
GROUP BY product_type, regist_date WITH ROLLUP;
+--------------+-------------+-----------+
| product_type | regist_date | sum_price |
+--------------+-------------+-----------+
| Office Supplies | 2009-09-11 | 500 |
| Office Supplies | 2009-11-11 | 100 |
| Office Supplies | NULL | 600 |
| Kitchenware | 2008-04-28 | 880 |
| Kitchenware | 2009-01-15 | 6800 |
| Kitchenware | 2009-09-20 | 3500 |
| Kitchenware | NULL | 11180 |
| clothes | NULL | 4000 |
| clothes | 2009-09-20 | 1000 |
| clothes | NULL | 5000 |
| NULL | NULL | 16780 |
+--------------+-------------+-----------+
11 rows in set (0.05 sec)
The result is zero :
here ROLLUP Yes product_type, regist_date Total two columns .
Exercises
5.1
Please name the... Used in this chapter product( goods ) The table is executed as follows SELECT The result of the statement .
mysql> SELECT product_id
,product_name
,sale_price
,MAX(sale_price) OVER (ORDER BY product_id) AS Current_max_price
FROM product;
+------------+--------------+------------+-------------------+
| product_id | product_name | sale_price | Current_max_price |
+------------+--------------+------------+-------------------+
| 0001 | T T-shirt | 1000 | 1000 |
| 0002 | Punch | 500 | 1000 |
| 0003 | motion T T-shirt | 4000 | 4000 |
| 0004 | kitchen knife | 3000 | 4000 |
| 0005 | pressure cooker | 6800 | 6800 |
| 0006 | Fork | 500 | 6800 |
| 0007 | Clean the board | 880 | 6800 |
| 0008 | Ball pen | 100 | 6800 |
+------------+--------------+------------+-------------------+
8 rows in set (0.08 sec)
5.2
Continue to use product surface , Calculate according to the registration date (regist_date) The sales unit price of each date in ascending order (sale_price) Total of . Sorting is required to set the registration date to NULL Of “ motion T T-shirt ” The record is at 1 position ( That is, think of it as earlier than other dates )
mysql> select
product_id,
product_name,
regist_date,
sale_price,
sum(sale_price)over(ORDER BY regist_date) as current_sum_price
from product;
+------------+--------------+-------------+------------+-------------------+
| product_id | product_name | regist_date | sale_price | current_sum_price |
+------------+--------------+-------------+------------+-------------------+
| 0003 | motion T T-shirt | NULL | 4000 | 4000 |
| 0007 | Clean the board | 2008-04-28 | 880 | 4880 |
| 0005 | pressure cooker | 2009-01-15 | 6800 | 11680 |
| 0002 | Punch | 2009-09-11 | 500 | 12180 |
| 0001 | T T-shirt | 2009-09-20 | 1000 | 16680 |
| 0004 | kitchen knife | 2009-09-20 | 3000 | 16680 |
| 0006 | Fork | 2009-09-20 | 500 | 16680 |
| 0008 | Ball pen | 2009-11-11 | 100 | 16780 |
+------------+--------------+-------------+------------+-------------------+
8 rows in set (0.10 sec)
5.3
Thinking questions
① The window function does not specify PARTITION BY What is the effect of ?
Do not specify a window , The default window is the whole table , for example :
-- Do not specify a window ,ranking Sort by the whole table
mysql> SELECT product_name
,product_type
,sale_price
,RANK() OVER (ORDER BY sale_price) AS ranking
FROM product;
+--------------+--------------+------------+---------+
| product_name | product_type | sale_price | ranking |
+--------------+--------------+------------+---------+
| Ball pen | Office Supplies | 100 | 1 |
| Punch | Office Supplies | 500 | 2 |
| Fork | Kitchenware | 500 | 2 |
| Clean the board | Kitchenware | 880 | 4 |
| T T-shirt | clothes | 1000 | 5 |
| kitchen knife | Kitchenware | 3000 | 6 |
| motion T T-shirt | clothes | 4000 | 7 |
| pressure cooker | Kitchenware | 6800 | 8 |
+--------------+--------------+------------+---------+
8 rows in set (0.05 sec)
-- Specify the window ,ranking Sort by window
mysql> SELECT product_name
,product_type
,sale_price
,RANK() OVER (PARTITION BY product_type
ORDER BY sale_price) AS ranking
FROM product;
+--------------+--------------+------------+---------+
| product_name | product_type | sale_price | ranking |
+--------------+--------------+------------+---------+
| Ball pen | Office Supplies | 100 | 1 |
| Punch | Office Supplies | 500 | 2 |
| Fork | Kitchenware | 500 | 1 |
| Clean the board | Kitchenware | 880 | 2 |
| kitchen knife | Kitchenware | 3000 | 3 |
| pressure cooker | Kitchenware | 6800 | 4 |
| T T-shirt | clothes | 1000 | 1 |
| motion T T-shirt | clothes | 4000 | 2 |
+--------------+--------------+------------+---------+
8 rows in set (0.07 sec)
② Why is it that window functions can only be used in SELECT Used in clauses ? actually , stay ORDER BY Clause using the system does not report an error .
Because the window function is right where perhaps group by Clause after processing the results of operation , So window functions can only be written in principle select clause
边栏推荐
- 【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
- Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
- 学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
- HCIP (01)
- 新能源销冠宏光MINIEV,有着怎样的产品力?
- SQL注入 Less23(过滤注释符)
- C# Newtonsoft. Jason advanced usage
- Use three.js to realize the cool cyberpunk style 3D digital earth large screen
- [递归] 938. 二叉搜索树的范围和
- Compressed list ziplist of redis
猜你喜欢

NowCoderTOP7-11——持续更新ing

HCIA experiment (07) comprehensive experiment

小微企业智能名片管理小程序

MySQL | GROUP_ The concat function concatenates the values of a column with commas
苹果美国宣布符合销售免税假期的各州产品清单细节

Common web attacks and defense

HCIA experiment (08)

NowCoderTOP12-16——持续更新ing

SQL injection less23 (filter comment)

Learn NLP with Transformer (Chapter 6)
随机推荐
Learn NLP with Transformer (Chapter 6)
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
B2B2C多商户系统功能丰富,极易二开!!!
HCIA experiment (06)
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
The B2B2C multi merchant system has rich functions and is very easy to open!!!
信息熵的定义
Hcip experiment (01)
Getting started with tensorflow
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Reptile foundation I
城市雕塑典型作品信息管理系统(图片分享系统SSM)
ArcMap cannot start the solution
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
Learn NLP with Transformer (Chapter 4)
Esp8266 uses drv8833 drive board to drive N20 motor
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
Learn NLP with Transformer (Chapter 5)