当前位置:网站首页>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 ..)
边栏推荐
- /*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
- 20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
- Introduction to canoe automatic test system
- R built in data set
- Article publishing experiment
- Locust installation
- Fundamentals of software testing
- 1. Circular nesting and understanding of lists
- [machine] [server] Taishan 200
- MFC document view framework (relationship between classes)
猜你喜欢
[Galaxy Kirin V10] [server] system partition expansion
Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
Dichotomy search (C language)
Unittest+airtest+beatiulreport combine the three to make a beautiful test report
[machine] [server] Taishan 200
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
[Galaxy Kirin V10] [desktop] FTP common scene setup
Postman interface test
C language - stack
[Galaxy Kirin V10] [server] NUMA Technology
随机推荐
Installation of ES plug-in in Google browser
[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing
51 data analysis post
Jemeter script recording
Canoe: the difference between environment variables and system variables
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Ten key performance indicators of software applications
Collection of practical string functions
Heartbeat启动后无反应
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
Canoe - description of common database attributes
First article
Canoe - the third simulation project - bus simulation - 3-2 project implementation
Day06 list job
Crawl Zhejiang industry and trade news page
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?
Summary of automated testing framework
Huge number multiplication (C language)
Jianzhi offer 04 (implemented in C language)