留存率的计算

留存率的计算_第1张图片



select starttime,round(base_1_num/base_num,2) as lcl_1,
       round(base_3_num/base_num,2) as lcl_3,
       round(base_7_num/base_num,2) as lcl_7
from (
         select starttime,count(distinct userid)as base_num
              ,count(distinct if(diff=1,userid,null)) as base_1_num
              ,count(distinct if(diff=3,userid,null)) as base_3_num
              ,count(distinct if(diff=7,userid,null)) as base_7_num
         from (
                  select t1.userid,t1.time as starttime,t2.time as endtime,datediff(t2.time,t1.time) as diff
                  from (
                           (select userid,date_format(time,'%Y-%m-%d') as time
                            from user_log
                            group by userid, date_format(time,'%Y-%m-%d')) t1
                               left join
                               (select userid,date_format(time,'%Y-%m-%d') as time
                                from user_log
                                group by userid, date_format(time,'%Y-%m-%d')) t2
                           on t1.userid=t2.userid and t2.time>t1.time
                           )
              )t3 group by starttime
     )t4

你可能感兴趣的:(数据库,sql)