Hive 累积和的计算

使用窗口函数Sum


eg:统计1-12月的累积销量,即1月为1月份的值,2月为1.2月份值的和,3月为123月份的和,12月为1-12月份值的和


SELECT 
month,
SUM(amount) month_amount,
SUM( SUM(amount)) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount
FROM 
table_name
GROUP BY month
ORDER BY month;


ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 定义起点和终点,UNBOUNDED PRECEDING 为起点,表明从第一行开始, CURRENT ROW为默认值,

就是这一句等价于:ROWS UNBOUNDED PRECEDING



ps:这个地方可以直接使用SUM( SUM(amount)) OVER (ORDER BY month) 

order by month 的默认值  order by month rows between unbounded preceding and current row

当然如果over子句里没有order 那么默认的窗口大小就是整个分组

你可能感兴趣的:(hive-实践)