彷徨 | Hive---报表统计

联级累计报表查询

有如下数据:

A,2015-01-08,5

A,2015-01-11,15

B,2015-01-12,5

A,2015-01-12,8

B,2015-01-13,25

A,2015-01-13,5

C,2015-01-09,10

C,2015-01-11,20

A,2015-02-10,4

A,2015-02-11,6

C,2015-01-12,30

C,2015-02-13,10

B,2015-02-10,10

B,2015-02-11,5

A,2015-03-20,14

A,2015-03-21,6

B,2015-03-11,20

B,2015-03-12,25

C,2015-03-10,10

C,2015-03-11,20

要求统计出如下累计报表:

用户

月份

月总额

累计到当月的总额

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

1 建表

建表映射:

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

row format delimited fields terminated by ',';

导入数据 :

2 月份数据切割聚合,只留到月份

select  username,substr(day,1,7) as month_sale,sum(msale) from t_sale
group by username,substr(day,1,7);

结果 :

3 自关联

select t1.*,t2.* from
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t1
join
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t2
on t1.username=t2.username;

结果 :

4 筛选

select t1.*,t2.* from
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t1
join
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t2
on t1.username=t2.username
where t1.month_sale>=t2.month_sale;

结果 : 

5 聚合统计

select t1.username,t1.month_sale,sum(t2.cnt) from
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t1
join
(select  username,substr(day,1,7) as month_sale,sum(msale) as cnt
from t_sale
group by username,substr(day,1,7)) t2
on t1.username=t2.username
where t1.month_sale>=t2.month_sale
group by t1.username,t1.month_sale;

结果 : 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值