hive级联累计

根据访问次数统计表,得到累计访问总计

  • 建表,load数据
t_access_times.dat
A,2015-01,5
A,2015-01,15
B,2015-01,5
A,2015-01,8
B,2015-01,25
A,2015-01,5
A,2015-02,4
A,2015-02,6
B,2015-02,10
B,2015-02,5

create table t_access_times(username string,month string,salary int)
row format delimited fields terminated by ',';

load data local inpath '/home/hadoop/t_access_times.dat' into table t_access_times;
+--------------------------+-----------------------+------------------------+--+
| t_access_times.username  | t_access_times.month  | t_access_times.salary  |
+--------------------------+-----------------------+------------------------+--+
| A                        | 2015-01               | 5                      |
| A                        | 2015-01               | 15                     |
| B                        | 2015-01               | 5                      |
| A                        | 2015-01               | 8                      |
| B                        | 2015-01               | 25                     |
| A                        | 2015-01               | 5                      |
| A                        | 2015-02               | 4                      |
| A                        | 2015-02               | 6                      |
| B                        | 2015-02               | 10                     |
| B                        | 2015-02               | 5                      |
+--------------------------+-----------------------+------------------------+--
  • 求每个用户的月总金额
select username,month,sum(salary) from t_access_times group by username,month;
+-----------+----------+------+--+
| username  |  month   | _c2  |
+-----------+----------+------+--+
| A         | 2015-01  | 33   |
| A         | 2015-02  | 10   |
| B         | 2015-01  | 30   |
| B         | 2015-02  | 15   |
+-----------+----------+------+--+
  • 把表自己inner join
select a.*,b.* 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
+-------------+----------+-----------+-------------+----------+-----------+--+
| a.username  | a.month  | a.salary  | b.username  | b.month  | b.salary  |
+-------------+----------+-----------+-------------+----------+-----------+--+
| A           | 2015-01  | 33        | A           | 2015-01  | 33        |
| A           | 2015-01  | 33        | A           | 2015-02  | 10        |
| A           | 2015-02  | 10        | A           | 2015-01  | 33        |
| A           | 2015-02  | 10        | A           | 2015-02  | 10        |
| B           | 2015-01  | 30        | B           | 2015-01  | 30        |
| B           | 2015-01  | 30        | B           | 2015-02  | 15        |
| B           | 2015-02  | 15        | B           | 2015-01  | 30        |
| B           | 2015-02  | 15        | B           | 2015-02  | 15        |
+-------------+----------+-----------+-------------+----------+-----------+--+
  • 生成累计值
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;

+-------------+----------+---------+-------------+--+
| a.username  | a.month  | salary  | accumulate  |
+-------------+----------+---------+-------------+--+
| A           | 2015-01  | 33      | 33          |
| A           | 2015-02  | 10      | 43          |
| B           | 2015-01  | 30      | 30          |
| B           | 2015-02  | 15      | 45          |
+-------------+----------+---------+-------------+

分组查询求月累计值。
为什么要max(salary)?
salary不是分组字段,只能由聚合函数求得,不然不知道选哪个。sum avg max

最后order by 使全局有序,原始数据无序,最后有可能无序。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,955评论 18 399
  • SQL语言基础 本章,我们将会重点探讨SQL语言基础,学习用SQL进行数据库的基本数据查询操作。另外请注意本章的S...
    厲铆兄阅读 10,704评论 2 46
  • 2017/3/14 RDBMS:关系型数据库管理系统 关系模型独立于语言 SQL有几种不同类型的语言:数据定义语言...
    ancherl阅读 5,581评论 0 6
  • 去年,网约车战场硝烟弥漫时,滴滴怎么也没想到,“边缘人”首汽约车悄然崛起、抄了滴滴后路。在各大平台忙于清除不合规格...
    Kickh阅读 2,924评论 0 0
  • 生命这条长河, 奔向远方, 没有方向,没有目的, 那样自由自在。 如果给它打上标号, 并不是祝愿, 那这一场旅行,...
    李译阅读 2,839评论 0 3