hive 的开窗函数sum() over() 函数

依然范特西╮ 提交于 2020-01-31 11:38:27

窗口分析函数 sum() over() :可以实现在窗口中进行逐行累加
测试数据:
这个是网上比较经典的案例了

2: jdbc:hive2://hadoop-03:10000> select * from t_access_amount;
+----------------------+------------------------+-------------------------+--+
| t_access_amount.uid  | t_access_amount.month  | t_access_amount.amount  |
+----------------------+------------------------+-------------------------+--+
| A                    | 2015-01                | 33                      |
| A                    | 2015-02                | 10                      |
| A                    | 2015-03                | 20                      |
| B                    | 2015-01                | 30                      |
| B                    | 2015-02                | 15                      |
| B                    | 2015-03                | 45                      |
| C                    | 2015-01                | 30                      |
| C                    | 2015-02                | 40                      |
| C                    | 2015-03                | 30                      |
+----------------------+------------------------+-------------------------+--+

要求:求出每个人截止到每个月的总额
即要得到如下表格

+------+----------+---------+-------------+--+
| uid  |  month   | amount  | accumulate  |
+------+----------+---------+-------------+--+
| A    | 2015-01  | 33      | 33          |
| A    | 2015-02  | 10      | 43          |
| A    | 2015-03  | 20      | 63          |
| B    | 2015-01  | 30      | 30          |
| B    | 2015-02  | 15      | 45          |
| B    | 2015-03  | 45      | 90          |
| C    | 2015-01  | 30      | 30          |
| C    | 2015-02  | 40      | 70          |
| C    | 2015-03  | 30      | 100         |
+------+----------+---------+-------------+--+

hql 语句

select uid,month,amount,
sum(amount) over(partition by uid order by month rows between unbounded preceding and current row) as accumulate
from t_access_amount;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!