题解 | #牛客每个人最近的登录日期(五)#

牛客每个人最近的登录日期(五)

https://www.nowcoder.com/practice/ea0c56cd700344b590182aad03cc61b8

select d.date,(case when retent_ratio is null then 0.000 else round(retent_ratio,3) end) as retent_ratio_final
from(select c.date, count(c.flag_add) as num_act,count(distinct b.user_id) as num_retent, count(distinct b.user_id)/count(c.flag_add) as retent_ratio
from (select a.user_id,a.date,(case when SUM(b.flag) != 1 then null else 1 end) as flag_add
from (
    select id,user_id,login.date,(case when user_id is not null then 1 end) as flag
    from login) AS a,(
    select id,user_id,login.date,(case when user_id is not null then 1 end) as flag
    from login) AS b
where a.user_id = b.user_id
and a.id>=b.id group by a.id,a.user_id) as c
left join login b
on c.user_id = b.user_id and c.flag_add is not null
and datediff(b.date,c.date) = 1 
group by c.date) as d

全部评论
重点是要使表格的形式为 用户id 对应的第一次登录的日期
点赞 回复 分享
发布于 2023-06-21 20:40 上海
写sql还是会容易将问题复杂化,还是不大熟悉其它函数的使用
点赞 回复 分享
发布于 2023-06-21 12:08 上海
没有想到用min函数来得到每名用户第一次登录的日期,尝试用累加的方式每名用户的第一次登陆日期标为1,其他为none。将问题复杂化了
点赞 回复 分享
发布于 2023-06-21 12:07 上海

相关推荐

嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
勤奋努力的椰子这就开摆:这些经历跟硬件都没啥关系呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务