题解 | #统计活跃间隔对用户分级结果#
select user_grade,format(nums*1.0/all_num,2) as ratio from ( select user_grade,count(1) as nums,sum(count(1)) over() as all_num from ( select uid, case when sum(case when out_date >=date_sub(max_date,interval 6 day) and is_new = 1 then 1 else 0 end) > 0 then '新晋用户' when sum(case when out_date >=date_sub(max_date,interval 6 day) then 1 else 0 end) > 0 then '忠实用户' when sum(case when out_date >=date_sub(max_date,interval 29 day) then 1 else 0 end) > 0 then '沉睡用户' else '流失用户' end as user_grade from ( select distinct uid, max_date, date_format(out_time,'%Y-%m-%d') as out_date, case when (dense_rank() over(partition by uid order by out_time)) = 1 then 1 else 0 end as is_new from tb_user_log a join (select date_format(max(out_time),'%Y-%m-%d') as max_date from tb_user_log) b ) t1 group by uid ) t2 group by user_grade ) t3 order by ratio desc,user_grade