打赏

12_Hive实战案例_累计报表_级联求和

注:Hive面试题:累积报表

数据文件:

  有如下访客访问次数统计表 t_access_times

  

  需要输出报表:t_access_times_accumulate

  

实现步骤:

  创建表,并将数据加载到表中:

  

  1、第一步,先求每个用户的月总金额

  

  

  2、第二步,将月总金额表 自己连接自己

  

  3、第三步,从上一步的结果中:进行分组查询,分组的字段是a.username a.month
                  求月累计值:将b.month <= a.month的所有b.salary求和即可

  

select A.username,A.month,max(A.salary) as salary,sum(B.salary) as accumulate
from 
(select username,month,sum(salary) as salary from t_access_times group by username,month) A 
inner join 
(select username,month,sum(salary) as salary from t_access_times group by username,month) B
on
A.username=B.username
where B.month <= A.month
group by A.username,A.month
order by A.username,A.month;
View Code

   最终结果

  

 

  

  

 

  

 

   

 

  



 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2018-07-13 10:07  QueryMarsBo  阅读(529)  评论(0编辑  收藏  举报