题解 | 最长连续登录天数

最长连续登录天数

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

-- 首先需要给登录日期排序
with temp as (
    select
        fdate,
        user_id,
        row_number() over(partition by user_id order by fdate asc) as rnk
        from (
            select
                distinct fdate, user_id
                from tb_dau
                WHERE fdate >= '2023-01-01' AND fdate <= '2023-01-31'
        ) t
),

temp1 as (
    select
        user_id,
        date_sub(fdate , interval rnk day) as grp_date,
        count(1) as total
        from temp
        group by user_id, DATE_SUB(fdate, INTERVAL rnk DAY)
)

select 
    user_id,
    max(total) as max_consec_days
    from temp1
    group by user_id

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-15 10:59
已编辑
爱写代码的菜code...:哎,自己当时拿到字节offer的时候也在感叹终于拿到了,自己当时最想去的企业就是字节,结果还是阴差阳错去了鹅厂。祝uu一切顺利!!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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