当前位置:网站首页>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
边栏推荐
- SQL injection less17 (error injection + subquery)
- Activity registration | play with kubernetes container service improvement class officially opened!
- Filter过滤器解决request请求参数乱码的原理解析
- Use three.js to realize the cool cyberpunk style 3D digital earth large screen
- Hcip experiment (02)
- tensorflow入门
- SQL language (II)
- 一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
- Some usages of beautifulsoup
- Learn NLP with Transformer (Chapter 6)
猜你喜欢

爬虫基础一

Let sports happen naturally, and fire creates a new lifestyle

SQL language (I)

Hcip experiment (03)

The most detailed MySQL index analysis (mind map is attached at the end of the article)

Code representation learning: introduction to codebert and other related models

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)

How to judge the performance of static code quality analysis tools? These five factors must be considered

There is a newline problem when passing shell script parameters \r

机智云物联网平台 STM32 ESP8266-01S 简单无线控灯
随机推荐
LVS load balancing lvs-nat building Web Cluster
Redis 入门
leetcode 剑指 Offer 27. 二叉树的镜像
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
基于MATLAB的常见线性调制方法
Why should the hashcode () method be rewritten when rewriting the equals () method
Learn NLP with Transformer (Chapter 3)
黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
【高并发】通过源码深度分析线程池中Worker线程的执行流程
Shell Chapter 5 homework
HCIP (01)
shell-第六章练习
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
Detailed explanation of zero basis from macro to micro Bert
Shell 脚本参数传递时有 \r 换行符问题
Compressed list ziplist of redis
My colleague looked at my code and exclaimed: how can I use a singleton in unity
Learn NLP with Transformer (Chapter 4)