SelectDB Cloud
SQL 手册
函数
window-functions
WINDOW-FUNCTION-SUM

WINDOW FUNCTION SUM

description

计算窗口内数据的和

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

example

按照 property 进行分组,在组内计算当前行以及前后各一行的x列的和。

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
© 2023 北京飞轮数据科技有限公司 京ICP备2022004029号 | Apache、Apache Doris 以及相关开源项目名称均为 Apache 基金会商标