题解 | #统计活跃间隔对用户分级结果#

统计活跃间隔对用户分级结果

https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af

with zb3 as (with zb2 as (with zb as (select uid,out_time,max(out_time) over() as max_time,/*离现在最近时间*/
first_value(out_time) over(partition by uid order by out_time desc) near_time,/*用户最近登录时间*/
first_value(out_time) over(partition by uid order by out_time) first_time /*用户第一次登录时间*/
from tb_user_log)
select *,datediff(max_time,near_time) diff_time /*最近时间和最近登录时间差*/
from zb
where out_time = near_time) /*去重,只留下最近登录时间,其他时间没有计算意义*/
select uid,(case when out_time=first_time and diff_time <=6 then '新晋用户' /*当最近登录时间和第一次登录时间一样,该用户则为新用户*/
			when diff_time <=6 and out_time!=first_time then '忠实用户'
            when diff_time <=29 then '沉睡用户' 
            else '流失用户' end) user_grade,count(*) over() sum_p 
from zb2)
select user_grade,round(count(uid)/max(sum_p),2) ratio
from zb3
group by user_grade
order by ratio desc,user_grade

全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务