当前位置:网站首页>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 .
边栏推荐
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
- aaaaaaaaaaaaa
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- The fourth provincial competition of Bluebridge cup single chip microcomputer
- The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
- In depth interpretation of pytest official documents (26) customized pytest assertion error information
- Kotlin basic learning 16
- 【DesignMode】原型模式(prototype pattern)
- 汇率的查询接口
猜你喜欢
![[mv-3d] - multi view 3D target detection network](/img/aa/741b36ead2dfaa5a165401b8d657b7.jpg)
[mv-3d] - multi view 3D target detection network

Getting started with MQ

The 10th Blue Bridge Cup single chip microcomputer provincial competition

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

Kubernetes cluster storageclass persistent storage resource core concept and use

The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

蓝桥杯单片机省赛第八届

Pycharm2021 delete the package warehouse list you added

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

Generate random numbers that obey normal distribution
随机推荐
Oracle common SQL
蓝桥杯单片机省赛第九届
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
[punch in] flip the string (simple)
5G时代全面到来,浅谈移动通信的前世今生
Merge interval, linked list, array
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
蓝桥杯单片机第六届温度记录器
蓝桥杯单片机省赛第八届
Eight steps of agile development process
In depth interpretation of pytest official documents (26) customized pytest assertion error information
[untitled] basic operation of raspberry pie (2)
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
蓝桥杯单片机省赛第十一届第二场
How about Ping An lifetime cancer insurance?
Basic syntax of unity script (7) - member variables and instantiation
Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
Oracle的md5
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
aaaaaaaaaaaaa
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