题解 | #最长连续登录天数#

最长连续登录天数

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

select c.user_id,max(c.cnt) as max_consec_days   
from
    (select b.user_id,count(*) as cnt
    from
        (select a.user_id,a.fdate,day(a.fdate)-ranking as cha
        from 
            (select user_id,fdate,
            row_number() over(partition by user_id order by fdate) as ranking 
            from tb_dau where year(fdate)=2023 and month(fdate) = 1) 
            as a )as b
    group by b.user_id,b.cha) as c
group by c.user_id

子表a:对每一个user_id的fdate进行排序标号

子表b:用日期中的日减去标号,如果是连续的日期,标号是一样的

子表c:用row_number对一样标号的序列进行重新标号,最大的值就是最大连续的天数

全部评论

相关推荐

牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
06-25 16:25
梧州学院 Java
愿汐_:项目介绍那么长,然而你做了啥就一句话?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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