当前位置:网站首页>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 ..)
边栏推荐
- Linked list operation can never change without its roots
- Introduction to canoe automatic test system
- Network connection (III) functions and similarities and differences of hubs, switches and routers, routing tables and tables in switches, why do you need address translation and packet filtering?
- 1. Circular nesting and understanding of lists
- Personal thoughts on the development of game automation protocol testing tool
- Virtual machine configuration network
- Seven examples to understand the storage rules of shaped data on each bit
- LVS+Keepalived实现四层负载及高可用
- I What is security testing
- [Galaxy Kirin V10] [server] system startup failed
猜你喜欢
Canoe - the third simulation project - bus simulation-1 overview
Postman interface test
Time complexity and space complexity
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
Ten key performance indicators of software applications
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
Canoe: what is vtsystem
Jianzhi offer 04 (implemented in C language)
VI text editor and user rights management, group management and time management
MFC document view framework (relationship between classes)
随机推荐
[Galaxy Kirin V10] [server] failed to start the network
When I forget how to write SQL, I
Elevator dispatching (pairing project) ②
[machine] [server] Taishan 200
Canoe - description of common database attributes
Heartbeat启动后无反应
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
2022 ape circle recruitment project (software development)
Hidden C2 tunnel -- use of icmpsh of ICMP
[Galaxy Kirin V10] [server] NUMA Technology
Performance test process
Dichotomy search (C language)
TS type gymnastics: illustrating a complex advanced type
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
XMIND installation
Using Lua to realize 99 multiplication table
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
Deepmind proposed a Zuan AI, which specially outputs network attack language