当前位置:网站首页>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 .
边栏推荐
- Oracle 查看被锁的表和解锁
- Retrofit's callback hell is really vulnerable in kotlin synergy mode
- The 5th Blue Bridge Cup single chip microcomputer provincial competition
- Didi open source Delta: AI developers can easily train natural language models
- Basic syntax of unity script (6) - specific folder
- The 7th Blue Bridge Cup single chip microcomputer provincial competition
- 一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
- Unity脚本的基础语法(6)-特定文件夹
- NLog use
- Unity脚本的基础语法(8)-协同程序与销毁方法
猜你喜欢
随机推荐
[designmode] builder model
Merge interval, linked list, array
Work hard all day long and be alert at sunset
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
蓝桥杯单片机省赛第十一届第一场
High performance and low power cortex-a53 core board | i.mx8m Mini
蓝桥杯单片机省赛第八届
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
Detailed explanation of ThreadLocal
Knowing things by learning | self supervised learning helps improve the effect of content risk control
The fourth provincial competition of Bluebridge cup single chip microcomputer
Kotlin basic learning 14
This article describes the step-by-step process of starting the NFT platform project
The 6th Blue Bridge Cup single chip microcomputer provincial competition
Blue Bridge Cup SCM digital tube skills
Review materials of project management PMP high frequency examination sites (8-1)
Kotlin 基础学习13
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Didi open source Delta: AI developers can easily train natural language models
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!