当前位置:网站首页>SQL learning window function
SQL learning window function
2022-04-23 13:45:00 【Du Xiaorui】
The window function is also called OLAP function .OLAP yes OnLine AnalyticalProcessing For short , It means real-time analysis and processing of database data .
The general form of window function :
< Window function > OVER ([ PARTITION BY < Name >]
ORDER BY
< Column names for sorting >)
- 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 .
for instance :
SELECT
product_name,
product_type,
sale_price,
RANK() OVER ( PARTITION BY product_type ORDER BY sale_price ) AS ranking
FROM
product
Here we use product_type Make a classification , Then sort the prices under each category .
Window function classification
Sort special window function
- RANK function
There is the same bit skip , Such as 1,2,2,4 - DENSE_RANK
Won't skip bits , Such as 1,1,1,2 - ROW_NUMBER
The same order will not appear , Even the same will be in different order
as follows :
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
The use of aggregate functions in Windows
Aggregate functions become cumulative when used in window functions :
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;
Calculate the moving average
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;
have access to PRECEDING and FOLLOWING To specify the scope of the aggregate function .
Like above ,ROWS 2 PRECEDING Indicates starting from the current line , Add the data of the first two lines of the current line , Then average .
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING Indicates the current line and the line before and after the current line , Then average .
give the result as follows :
GROUPING Operator
ROLLUP - Calculate total and subtotal
SELECT product_type
,regist_date
,SUM(sale_price) AS sum_price
FROM product
GROUP BY product_type, regist_date WITH ROLLUP
Exercises
5.1
The execution result of the following statement
SELECT product_id
,product_name
,sale_price
,MAX(sale_price) OVER (ORDER BY product_id) AS Current_max_price
FROM product
The execution result of this statement is the highest selling price in all current records from top to bottom .
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 )
SELECT
product_id,
product_name,
sale_price,
sum( sale_price ) OVER (
ORDER BY
IF
( ISNULL( regist_date ), 0, 1 )) AS Current_sum_price
FROM
product;
5.3
Thinking questions
① The window function does not specify PARTITION BY What is the effect of ?
② 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 .
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556584684.html
边栏推荐
- MySQL and PgSQL time related operations
- The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
- Django::Did you install mysqlclient?
- Ai21 labs | standing on the shoulders of giant frozen language models
- Handling of high usage of Oracle undo
- Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
- RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
- 浅谈js正则之test方法bug篇
- About me
- Antd design form verification
猜你喜欢
ACFs file system creation, expansion, reduction and other configuration steps
Oracle job scheduled task usage details
【vmware】vmware tools 地址
集简云 x 飞书深诺,助力企业运营部实现自动化办公
Usereducer basic usage
OSS cloud storage management practice (polite experience)
Technologie zéro copie
Opening: identification of double pointer instrument panel
kettle庖丁解牛第16篇之输入组件周边讲解
PG SQL intercepts the string to the specified character position
随机推荐
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
聯想拯救者Y9000X 2020
JS time to get this Monday and Sunday, judge the time is today, before and after today
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
Short name of common UI control
Common types and basic usage of input plug-in of logstash data processing service
10g database cannot be started when using large memory host
Aicoco AI frontier promotion (4.23)
Window function row commonly used for fusion and de duplication_ number
Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
Processbuilder tool class
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
Remove the status bar
[multi screen interaction] realize dual multi screen display II: startactivity mode
浅谈js正则之test方法bug篇
kettle庖丁解牛第16篇之输入组件周边讲解
爱可可AI前沿推介 (4.23)
校园外卖系统 - 「农职邦」微信原生云开发小程序
Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
Two ways to deal with conflicting data in MySQL and PG Libraries