第1题 —— 数据库操作
表ord(用户订单表)
| user_id | ord_id | ord_amt | create_time(预定时间) |
|---|---|---|---|
| 1 | 001 | 888 | 2019-05-01 |
| 1 | 002 | 367 | 2019-05-03 |
| 1 | 003 | 500 | 2019-05-04 |
| 1 | 004 | 987 | 2019-05-11 |
| 1 | 005 | 769 | 2019-05-12 |
| 205 | 009 | 209 | 2019-05-07 |
| 205 | 010 | 780 | 2019-05-08 |
| 205 | 020 | 998 | 2019-05-12 |
表act_usr(活动参与用户表)
| act_id | user_id | create_time(报名时间) |
|---|---|---|
| Act_1 | 1 | 2019-05-03 |
| Act_2 | 80 | 2019-05-06 |
| Act_3 | 205 | 2019-05-07 |
(1)创建表act_output,保存以下信息:
区分不同活动,统计每个活动对应所有用户在报名参与活动之后产生的总订单金额、总订单数(一个用户只能参加一个活动)。
create table act_output asselect act_id,count(ord_id) as '总订单数',sum(ord_amt)as'总金额'from act_usr aleft join ord oon a.user_id=o.user_id and a.create_time<=o.create_timegroup by act_id
(2)加入活动开始后每天都会产生订单,计算每个活动截止当前(测评当天)平均每天产生的订单数,活动开始时间假设为用户最早报名时间。
select act_id,count(ord_id) as '总订单数',datediff('2021-01-01',min(a.create_time))+1 as '时间间隔',count(ord_id)/(datediff('2021-01-01',min(a.create_time))+1)as '平均每日产生的订单数'from act_usr aleft join ord oon a.user_id=o.user_id and a.create_time<=o.create_timegroup by act_id
第2题 —— 数据库操作
某网络用户访问操作流水表 tracking_log,
| user_id | opr_type(操作类型) | log_time(操作时间) |
|---|---|---|
| 1 | A | 2019-05-01 |
| 1 | B | 2019-05-01 |
| 1 | C | 2019-05-01 |
| 1 | B | 2019-05-07 |
| 1 | E | 2019-05-08 |
| 2 | A | 2019-05-06 |
| 2 | B | 2019-05-06 |
(1)计算网站每天的访客数以及他们的平均操作次数;
select date(log_time),#date()将datetime转化为datecount(distinct user_id)as '访客数',count(*)/count(distinct user_id) as '平均操作次数'from tracking_loggroup by date(log_time)
(2)统计每天符合A操作后B操作的操作模式的用户数,即要求AB相邻。
select date(log_time),count(distinct user_id)from (select user_id,opr_type,log_time,lead(opr_type)over(partition by user_id,log_time order by log_time) as next_opr_typefrom tracking_log) as t1where opr_type='A' and next_opr_type='B'group by date(log_time)
3)统计用户行为序列为A-B-D的用户数
其中:A-B之间可以有任何其他浏览记录(如C,E等),B-D之间除了C记录可以有任何其他浏览记录(如A,E等)
group_concat()是研究行为路径“顺序”的万能方案
注意⚠️:问清楚是abd连续的操作,还是只要求三者顺序,中间可以还有其他操作**
select count(*)from (select user_id, group_concat(opr_type order by log_time separator '-') as user_behaviorfrom tracking_loggroup by user_idhaving user_behavior like '%A%B%D%' and user_behavior not like '%A%B%C%D%') as t1
第3题 —— 数据库操作
根据第2题的用户访问操作流水表 tracking_log,
(1)计算网络每日新增访客表(在这次访问之前没有访问过该网站);
select reg, count(distinct user_id) as '新增访客数'from (select user_id,min(log_time) as regfrom tracking_loggroup by user_id) as t1group by reg
(2)新增访客的第2日、第30日回访比例。
create table register_log asselect user_id,min(log_time) as regfrom tracking_loggroup by user_idselect reg, '时间间隔', '留存数'/'新增访客数' as '回访比例'from (select reg,count(distinct user_id) as '新增访客数'from register_loggroup by reg) as t1join (select reg,datediff(t.log_time,reg)as '时间间隔',count(distinct user_id) as '留存数'from register_log r join tracking_log t on r.user_id=t.user_idwhere datediff(t2.log_time,reg)=1 or datediff(t2.log_time,reg)=29group by reg,t.log_time) as t2on t1.reg=t2.regorder by reg,'时间间隔';
