依据对用户 ID、拜访日期、拜访次数的若干条数据,剖析得出每个用户的累计拜访次数,波及字符串替换、工夫函数、开窗、求和等技术。
userId |
visitDate |
visitCount |
u01 |
2017/1/21 |
5 |
u02 |
2017/1/23 |
6 |
u03 |
2017/1/22 |
8 |
u04 |
2017/1/20 |
3 |
u01 |
2017/1/23 |
6 |
u01 |
2017/2/21 |
8 |
u02 |
2017/1/23 |
6 |
u01 |
2017/2/22 |
4 |
用户 id |
月份 |
小计 |
累积 |
u01 |
2017-01 |
11 |
11 |
u01 |
2017-02 |
12 |
23 |
u02 |
2017-01 |
12 |
12 |
u03 |
2017-01 |
8 |
8 |
u04 |
2017-01 |
3 |
3 |
create table action
(userId string,
visitDate string,
visitCount int)
row format delimited fields terminated by "\t";
load data local inpath '/data/action.dat' into table action;
select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action;
userid |
mn |
visitcount |
u01 |
2017-01 |
5 |
u02 |
2017-01 |
6 |
u03 |
2017-01 |
8 |
u04 |
2017-01 |
3 |
u01 |
2017-01 |
6 |
u01 |
2017-02 |
8 |
u02 |
2017-01 |
6 |
u01 |
2017-02 |
4 |
select
userId,
mn,
sum(visitCount) mn_count
from
(select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action) as t1
group by
userId,mn;
userid |
mn |
mn_count |
u01 |
2017-01 |
11 |
u01 |
2017-02 |
12 |
u02 |
2017-01 |
12 |
u03 |
2017-01 |
8 |
u04 |
2017-01 |
3 |
select
userId,
mn,
mn_count,
sum(mn_count) over(partition by userId order by mn)
from (select
userId,
mn,
sum(visitCount) mn_count
from
(select
userId,
date_format(regexp_replace(visitDate,'/','-'),'yyyy-MM') mn,
visitCount
from
action) as t1
group by
userId,mn) as t2;
userid |
mn |
mn_count |
sum_window_0 |
u01 |
2017-01 |
11 |
11 |
u01 |
2017-02 |
12 |
23 |
u02 |
2017-01 |
12 |
12 |
u03 |
2017-01 |
8 |
8 |
u04 |
2017-01 |
3 |
3 |