Window aggregate
Use window aggregate to compute averages, sort, rank, or count items, calculate sums, and find minimum or maximum values across window.
Window aggregate overview
Window aggregate syntax
windowedAggCall:
windowAggFunction
OVER
window
windowAggFunction:
agg '(' [ ALL | DISTINCT ] value [, value ]* ')'
|
agg '(' '*' ')'
window:
'('
[ PARTITION BY expression [, expression ]* ]
[ ORDER BY orderItem [, orderItem ]* ]
[
RANGE numericOrIntervalExpression { PRECEDING | FOLLOWING }
| ROWS numericExpression { PRECEDING | FOLLOWING }
]
')'Example window aggregate query layout
Window mechanism (OVER clause)
Partition by clause
Order by clause
Frame clause
Window aggregate 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
Was this helpful?

