10.5 课后练习
10.5.1 第一题
我们有如下的用户访问数据
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 |
要求使用sql统计出每个用户的累计访问次数,如下表所示
用户id | 月份 | 小计 | 累计 |
---|---|---|---|
u01 | 2027-01 | 11 | 11 |
u01 | 2027-02 | 12 | 23 |
u02 | 2027-01 | 12 | 12 |
u03 | 2027-01 | 8 | 8 |
u04 | 2027-01 | 3 | 3 |
创建表
create table action( user_id string, visit_date string, visit_count int ) row format delimited fields terminated by '\t';
加载数据
[atguigu@hadoop102 data]$ vim action.txt --- 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 ---
load data local inpath '/opt/module/hive-3.1.2/data/action.txt' into table action;
查询语句SQL作答
select user_id, year_month, visit_subtotal, sum(visit_subtotal) over(partition by user_id order by visit_subtotal desc) visit_total from (select user_id, year_month, sum(visit_count) as visit_subtotal from (select user_id, date_format(regexp_replace(visit_date, '/', '-'), 'yyyy-MM') year_month, visit_count from action) t group by user_id, year_month) t2;
10.5.2 第二题
有50W个京东店铺,每个顾客访问任何一个店铺的任何一个商品时都会产生一条访问日志,访问日志存储的表名为visit,访问的客户id为 user_id,被访问的店铺名称为shop,请统计
① 每个店铺的UV(访客数)
② 每个店铺访问次数top3的访客信息。输出店铺名称、访客id、访客次数
建表
create table visit(user_id string, shop string) row format delimited fields terminated by '\t';
加载数据