当前位置:网站首页>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 ..)
边栏推荐
- Sword finger offer 05 (implemented in C language)
- Jemeter plug-in technology
- 20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
- Deepmind proposed a Zuan AI, which specially outputs network attack language
- Write a program that uses pointers to set all elements of an int array to 4.18: 0.
- Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
- Design and common methods of test case documents
- For and while loops
- Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
- JMeter correlation technology
猜你喜欢
![[Galaxy Kirin V10] [server] FTP introduction and common scenario construction](/img/ef/f0f722aaabdc2d98723cad63d520e0.jpg)
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction

Using SA token to solve websocket handshake authentication

Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of

Function introduction of canbedded component
![[Galaxy Kirin V10] [desktop and server] FRP intranet penetration](/img/54/7d1b8b7b8226e2820635dd04b92d1c.jpg)
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration

Canoe - the second simulation project -xvihicle1 bus database design (operation)

2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
![[Galaxy Kirin V10] [server] NUMA Technology](/img/9b/65466c6fc6336e27e842f50c26b9c3.jpg)
[Galaxy Kirin V10] [server] NUMA Technology

VI text editor and user rights management, group management and time management

How to use diff and patch to update the source code
随机推荐
[test theory] test phase analysis (unit, integration, system test)
Get the data of the top 100 headlines today with Tianxing data
Canoe - the third simulation project - bus simulation - 3-2 project implementation
[Galaxy Kirin V10] [server] system startup failed
Seven examples to understand the storage rules of shaped data on each bit
Snake (C language)
TS type gymnastics: illustrating a complex advanced type
Design and common methods of test case documents
Communication layer of csframework
LVS+Keepalived实现四层负载及高可用
shell awk
Elevator dispatching (pairing project) ①
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
JMeter common configuration components and parameterization
C language - stack
2、 Operators and branches
Canoe: distinguish VT, VN and vteststudio from their development history
JMeter Foundation
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
Four characteristics and isolation levels of database transactions