数据分析训练营-阶段二模块二作业

1.0.建立用户活跃日期表

代码如下:

 create table temep_user_act

     (user_id int,

     dates date);


运行并导入数据后截图:


建表及导入的数据截图

1.1.用用户活跃日期表做自连接,连接方式使用左连接,连接字段使用“用户id”字段,保留两表的用户id与两表的日期(20分)

代码(使用视图):

 CREATE VIEW temp_user_act_view AS

 SELECT

   a.user_id as a_user_id,

   b.user_id as b_user_id,

   a.dates as a_dates,

   b.dates as b_dates

FROM

   temp_user_act a

LEFT JOIN temp_user_act b ON a.user_id = b.user_id;

结果截图:


运行结果视图截图

1.2.筛选出右表日期大于或等于左表日期的内容(20分)

代码(使用视图):

CREATE VIEW dates_choose_view AS

select*from temp_user_act_view

where b_dates>=a_dates;

结果截图:


筛选内容结果截图

1.3.计算以左表日期为基准日的当日用户数,第二日回访用户数,第三日回访用户数,第四日回访用户数,第八日回访用户数;(20分)

代码(使用视图):

CREATE VIEW  fangwenshu_view AS

select

  a_dates,

 COUNT(DISTINCT a_user_id) as 'day0',

  count(distinct if(datediff(b_dates,a_dates)=1,a_user_id,null)) as 'day1',

  count(distinct if(datediff(b_dates,a_dates)=2,a_user_id,null)) as 'day2',

count(distinct if(datediff(b_dates,a_dates)=3,a_user_id,null)) as 'day3',

  count(distinct if(datediff(b_dates,a_dates)=4,a_user_id,null)) as 'day4',

count(distinct if(datediff(b_dates,a_dates)=5,a_user_id,null)) as 'day5',

count(distinct if(datediff(b_dates,a_dates)=6,a_user_id,null)) as 'day6',

count(distinct if(datediff(b_dates,a_dates)=7,a_user_id,null)) as 'day7'

from dates_choose_view

GROUP BY a_dates

ORDER BY a_dates;

结果截图:


访问数结果截图

1.4.利用上述数据计算出每日的当日用户数以及次日留存率,二日留存率,三日留存率,7日留存率(率需要使用百分比表示结果);(20分)

代码(使用视图):

CREATE VIEW liucunlv_view AS

select

  a_dates,

concat(round((day1/day0)*100,2),'%') as '第一日留存率',

concat(round((day2/day0)*100,2),'%') as '第二日留存率',

concat(round((day3/day0)*100,2),'%') as '第三日留存率',

concat(round((day4/day0)*100,2),'%') as '第四日留存率',

concat(round((day5/day0)*100,2),'%') as '第五日留存率',

concat(round((day6/day0)*100,2),'%') as '第六日留存率',

concat(round((day7/day0)*100,2),'%') as '第七日留存率'

from fangwenshu_view

GROUP BY a_dates;

结果截图:


留存率结果截图

你可能感兴趣的:(数据分析训练营-阶段二模块二作业)