WINDOW FUNCTION AVG
Description
This function calculates the average of the data within the window.
AVG([DISTINCT | ALL] *expression*) [OVER (*analytic_clause*)]
Example
Calculate the average of data in the current row and the rows above and below it.
select x, property,
avg(x) over
(
partition by property
order by x
rows between 1 preceding and 1 following
) as 'moving average'
from int_t where property in ('odd','even');
| x | property | moving average |
|----|----------|----------------|
| 2 | even | 3 |
| 4 | even | 4 |
| 6 | even | 6 |
| 8 | even | 8 |
| 10 | even | 9 |
| 1 | odd | 2 |
| 3 | odd | 3 |
| 5 | odd | 5 |
| 7 | odd | 7 |
| 9 | odd | 8 |
Keywords
WINDOW,FUNCTION,AVG