VeloDB Cloud
SQL Manual
Functions
window-functions
WINDOW-FUNCTION-SUM

WINDOW FUNCTION SUM

Description

This function calculates the sum of the data within the window.

SUM([DISTINCT | ALL] expression) [OVER (analytic_clause)]

Example

Group the data by the property column, and calculate the sum of all the x in the current row and the rows above and below it within the group.

select x, property,   
sum(x) over    
(   
partition by property   
order by x   
rows between 1 preceding and 1 following    
) as 'moving total'    
from int_t where property in ('odd','even');
 
| x  | property | moving total |
|----|----------|--------------|
| 2  | even     | 6            |
| 4  | even     | 12           |
| 6  | even     | 18           |
| 8  | even     | 24           |
| 10 | even     | 18           |
| 1  | odd      | 4            |
| 3  | odd      | 9            |
| 5  | odd      | 15           |
| 7  | odd      | 21           |
| 9  | odd      | 16           |

Keywords

WINDOW,FUNCTION,SUM