Window Functions
Use window functions to compute averages, sort, rank, or count items, calculate sums, and find minimum or maximum values across windows.
Window Functions overview
Window function syntax
windowedCall:
windowFunction
OVER
window
windowFunction:
function_name '(' value [, value ]* ')'
|
function_name '(' '*' ')'
window:
'('
[ PARTITION BY expression [, expression ]* ]
[ ORDER BY orderItem [, orderItem ]* ]
[
RANGE BETWEEN frame_start AND frame_end
|
ROWS BETWEEN frame_start AND frame_end
|
RANGE frame_start
|
ROWS frame_start
]
')'
frame_start:
UNBOUNDED PRECEDING
|
offset PRECEDING
|
CURRENT ROW
|
offset FOLLOWING
frame_end:
offset PRECEDING
|
CURRENT ROW
|
offset FOLLOWING
|
UNBOUNDED FOLLOWING Example window function query layout
Window mechanism (OVER clause)
Partition by clause
Order by clause
Frame clause
Window functions
Function
Description
Example
Default Value When No Record Selected
Window aggregate query examples
Sum transactions by customer ID
customer_id
payment_date
amount
sum
Find the minimum or maximum transaction by customer ID
customer_id
payment_date
amount
min
0.99
0.99
0.99
4.99
4.99
2.99
2.99
2.99
Find the average transaction amount by customer ID
customer_id
payment_date
amount
avg
5.66
5.66
5.66
7.99
7.99
3.99
3.99
3.99
Rank year-to-date sales for a sales team
Row
FirstName
LastName
Total sales YTD
2251368.34
2151341.64
1551363.54
1251358.72
Count the number of transactions by customer ID
customer_id
payment_date
amount
count
Last updated
Was this helpful?

