题解 | 最长连续登录天数 - 刷一下这道面试高频题

最长连续登录天数

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

with t1 as (
    select distinct
        *
    from
        tb_dau
    order by
        user_id,
        fdate
),
t2 as (
    select
        user_id,
        fdate,
        row_number() over (partition by user_id order by fdate) as rn
    from
        t1
),
t3 as (
    select
        user_id,
        date_sub(fdate, interval rn day) as a
    from
        t2
),
t4 as (
    select
        user_id,
        count(*) as cdate
    from
        t3
    group by
        user_id,
        a
)
select
    user_id,
    max(cdate) as max_consec_days
from
    t4
group by
    user_id

感觉写繁琐了

全部评论

相关推荐

03-27 01:58
已编辑
西北工业大学 Java
在平静中度过当下:如果这个bg也简历挂的话可能他们现在不缺人了吧,我也是这两天投的,阿里和快手投的岗都是简历秒挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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