级联累计查询(基于hive)
原始数据类型
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
cd /opt
mkdir my_test
touch jilianqiuhe.dat
vi jilianqiuhe.dat
create table t_access_times(username string,month string,salary int)
row format delimited fields terminated by ',';
load data local inpath '/opt/my_test/jilianqiuge.dat' into table t_access_times;
hive>select * from 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 |
+
hive>select username,month,sum(salary) from t_access_times group by username,month;
- 结果

- 自己 inner join
hive>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
- 结果

- 生成累加值
hive>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;
- 结果
