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

窗口分析函数 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;
发布了58 篇原创文章 · 获赞 10 · 访问量 3462

猜你喜欢

转载自blog.csdn.net/weixin_43548518/article/details/104117054