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

C,2015-01,10

C,2015-01,20

A,2015-02,4

A,2015-02,6

C,2015-02,30

C,2015-02,10

B,2015-02,10

B,2015-02,5

A,2015-03,14

A,2015-03,6

B,2015-03,20

B,2015-03,25

C,2015-03,10

C,2015-03,20

 

建表:

create table t_access_times(username string,month string,counts int)

row format delimited fields terminated by ',';

开发脚本:以致最后结果如下:

用户

月份

月总额

累计到当月的总额

A

2015-01

33

33

A

2015-02

10

43

A

2015-03

30

73

B

2015-01

30

30

B

2015-02

15

45

 

首先,查询没人每月的总额

create table t_access_amount

as

select uid,month,sum(times) as amount

from t_access_times

group by uid,month;

将总额表自连接

create table t_tmp

as

select a.amount as a_amount,b.*

from t_access_amount a join t_access_amount b

on a.uid=b.uid;

where a.month <= b.month;

最终结果:

select uid,month,max(amount) as amount, sum(a_amount) as accumulate

from t_tmp

group by  uid,month;

你可能感兴趣的:(Hive)