当前位置:网站首页>SQL Yiwen get window function
SQL Yiwen get window function
2022-07-02 03:39:00 【Xiao Wang next door to you】
Written above
- Author's brief introduction : Hello everyone , I'm Xiao Wang *️
- Personal home page : Xiao Wang next door to you
- Welcome to thumb up + Collection ️+ Leaving a message.
- special column :SQL*️
*️ If you have something you don't understand in the process of learning , Welcome to the comment area to leave a message and ask questions ! I hope I can make progress with you , Grow up together !
Catalog
Basic usage of grammar —— Use RANK function
Types of special window functions
Window function
What is a window function
- The window function is also called OLAP function . In order to let everyone quickly form an intuitive impression , Just got such an easy to understand name ,OLAP yes OnLine Analytical Processing For short , It means real-time analysis and processing of database data .
Syntax of window function
< Window function > OVER ([ PARTITION BY < Make a list >]
ORDER BY < Sort by listing >)
① Aggregate function that can be used as window function ( SUM 、 AVG 、 COUNT 、 MAX 、 MIN )
② RANK 、 DENSE _ RANK 、 ROW _ NUMBER And so on
Basic usage of grammar —— Use RANK function
![]()
about Product In the table 8 Commodity , According to different kinds of goods ( product_type ), According to the sales unit price ( sale_price ) Sort from low to high
SELECT product_name, product_type, sale_price, RANK () OVER (PARTITION BY product_type ORDER BY sale_price) AS ranking FROM Product;
PARTITION BY Can set the range of sorting objects ,ORDER BY Can specify which column to follow 、 In what order . PARTITION BY Group tables horizontally , and ORDER BY Determines the vertical sorting rules .
The key to using window functions is PARTITION BY and GROUP BY . among , PARTITION BY It's not necessary , Even if it is not specified, the window function can be used normally .
SELECT product_name, product_type, sale_price,
RANK () OVER (ORDER BY sale_price) AS ranking
FROM Product;
![]()
Types of 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 ……
ps:( Special window functions do not require parameters , Therefore, the brackets are usually empty , In principle, window functions can only be used in SELECT Used in clauses , stay SELECT Out of clause “ Using window functions is meaningless ”)
Calculate the moving average
Window function is to divide the table into windows , And sort in it . In fact, it also includes the alternative function of specifying a more detailed summary range in the window , The summary scope in this alternative function is called the framework
SELECT product_id, product_name, sale_price, AVG (sale_price) OVER (ORDER BY product_id ROWS 2 PRECEDING) AS moving_avg FROM Product;
ROWS (“ That's ok ”) and PRECEDING (“ Before ”) Two keywords , Specify the frame as “ By the end of ~ That's ok ”,“ ROWS 2 PRECEDING ” Is to specify the frame as “ By the end of 2 That's ok ”, That is, the records as the summary object are limited to the following “ The closest 3 That's ok ”, That is, oneself 、 Before 1 That's ok 、 The first two lines
keyword FOLLOWING (“ after ”) Replace PRECEDING , You can specify “ Until after ~ That's ok ” As a framework
If you want to use the front and back lines of the current record as summary objects , It needs to be used at the same time PRECEDING (“ Before ”) and FOLLOWING (“ after ”) Keyword to implement .
SELECT product_id, product_name, sale_price, AVG (sale_price) OVER (ORDER BY product_id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS moving_avg FROM Product;
Two ORDER BY
- When using window functions, you must be in OVER Used in clauses ORDER BY , OVER In Clause ORDER BY It is only used to determine the order in which window functions are calculated , The order of the results does not affect the use of the aggregate function as a window function , The record of the summary object will be determined based on the current record .
边栏推荐
- 蓝桥杯单片机省赛第五届
- Kotlin basic learning 16
- Global and Chinese markets for infant care equipment, 2022-2028: Research Report on technology, participants, trends, market size and share
- Eight steps of agile development process
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- aaaaaaaaaaaaa
- 蓝桥杯单片机省赛第十一届第二场
- "Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
- Network connection mode of QT
- Blue Bridge Cup SCM digital tube skills
猜你喜欢
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
ThreadLocal详解
[HCIA continuous update] overview of dynamic routing protocol
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
蓝桥杯单片机省赛第十一届
蓝桥杯单片机第四届省赛
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
随机推荐
h5中的页面显示隐藏执行事件
Go execute shell command
蓝桥杯单片机数码管技巧
ImageAI安装
Oracle的md5
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
Kotlin basic learning 15
Oracle viewing locked tables and unlocking
汇率的查询接口
蓝桥杯单片机省赛第五届
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Exchange rate query interface
Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
Kotlin基础学习 14
潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
Class design basis and advanced
[mv-3d] - multi view 3D target detection network
蓝桥杯单片机省赛第十一届第一场