当前位置:网站首页>Getting started with window functions
Getting started with window functions
2022-07-04 10:57:00 【ths512】
1. When to use windowing function ?
Windowing functions are often used in conjunction with aggregate functions , Generally speaking, the number of rows after aggregation is less than that before aggregation , But sometimes we want to show the data before aggregation , And display the aggregated data , Now we introduce the window function .
for example :
+-------+-------------+-------+---------------+--+
| name | orderdate | cost | sum_window_0 |
+-------+-------------+-------+---------------+--+
| jack | 2017-01-01 | 10 | 205 |
| jack | 2017-01-08 | 55 | 205 |
| tony | 2017-01-07 | 50 | 205 |
| jack | 2017-01-05 | 46 | 205 |
| tony | 2017-01-04 | 29 | 205 |
| tony | 2017-01-02 | 15 | 205 |
| jack | 2017-02-03 | 23 | 23 |
| mart | 2017-04-13 | 94 | 341 |
| jack | 2017-04-06 | 42 | 341 |
| mart | 2017-04-11 | 75 | 341 |
| mart | 2017-04-09 | 68 | 341 |
| mart | 2017-04-08 | 62 | 341 |
| neil | 2017-05-10 | 12 | 12 |
| neil | 2017-06-12 | 80 | 80 |
+-------+-------------+-------+---------------+--
2. Syntax of window function :
UDAF() over (PARTITION By col1,col2 order by col3 Window clause (rows between .. and ..)) AS Column alias
Be careful :PARTITION By It can be followed by multiple fields ,order By Just follow one field .
3.over() The role of
over() Determines the aggregation range of the aggregation function , By default, the data in the whole window is aggregated , The aggregate function calls each piece of data once .
for example :
select name, orderdate, cost, sum(cost) over()
from business;
4.partition by Clause :
Use Partiton by Clause to partition data , It can be used paritition by Aggregate the in the area .
for example :
select name, orderdate, cost, sum(cost) over(partition by name)
from business;
5.order by Clause :
effect :
(1) Sort the data in the partition ;
(2) Determine which rows to aggregate ( By default, the aggregation from the starting point to the current row )
for example :
select name, orderdate, cost, sum(cost) over(partition by name order by orderdate)
from business;
6. Window clause
CURRENT ROW: Current row
n PRECEDING: forward n Row data
n FOLLOWING: Back up n Row data
UNBOUNDED: The starting point ,UNBOUNDED PRECEDING From the front , UNBOUNDED FOLLOWING To the end
By using partition by Clause partitions the data . If you want to make a more detailed dynamic division of windows ,
We need to introduce window clauses .
for example :
select name, orderdate,cost, sum(cost)
over(partition by name order by orderdate rows between UNBOUNDED PRECEDING and CURRENT ROW)
from business;
7. Some attention :
(1)order by Must be with the partition by after ;
(2)Rows Must be with the Order by Son ;
(3)(partition by .. order by) Can be replaced by (distribute by .. sort by ..)
边栏推荐
- [Galaxy Kirin V10] [desktop] can't be started or the screen is black
- Function introduction of canbedded component
- Elevator dispatching (pairing project) ②
- Get the data of the top 100 headlines today with Tianxing data
- Performance test method
- Appscan installation steps
- Using SA token to solve websocket handshake authentication
- Canoe test: two ways to create CAPL test module
- SSH原理和公钥认证
- I What is security testing
猜你喜欢

C language structure to realize simple address book

How do microservices aggregate API documents? This wave of show~

Ten key performance indicators of software applications

Appscan installation error: unable to install from Net runtime security policy logout appscan solution

Four characteristics and isolation levels of database transactions

Send a request using paste raw text

DNS hijacking

Canoe - the third simulation project - bus simulation-1 overview

Jianzhi offer 04 (implemented in C language)
![[Galaxy Kirin V10] [desktop] cannot add printer](/img/a6/28e4aa31e805a018e6db2b32ca1be0.jpg)
[Galaxy Kirin V10] [desktop] cannot add printer
随机推荐
Linked list operation can never change without its roots
[Galaxy Kirin V10] [server] system partition expansion
Locust learning record I
Canoe - the second simulation project -xvihicle1 bus database design (operation)
What is an excellent architect in my heart?
SSH原理和公钥认证
Huge number (C language)
Time complexity and space complexity
Canoe - description of common database attributes
Dictionaries and collections
Seven examples to understand the storage rules of shaped data on each bit
Interview and lecture summary 1
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
[Galaxy Kirin V10] [server] grub default password
[test theory] test process management
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
[Galaxy Kirin V10] [desktop] printer
Capl: timer event
Learning XML DOM -- a typical model for parsing XML documents
Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.