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

最长连续登录天数

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

WITH
t1 AS (
SELECT
    user_id,
    fdate,
    RANK()OVER(PARTITION BY user_id ORDER BY fdate) AS rk
FROM tb_dau
WHERE fdate BETWEEN '2023-01-01' AND '2023-01-31'
)

SELECT
    user_id,
    MAX(cnt) AS max_consec_days
FROM
(
SELECT
    user_id,
    DATE_SUB(fdate, INTERVAL rk DAY) AS sdate,
    COUNT(1) AS cnt
FROM t1
GROUP BY user_id, sdate
) AS m
GROUP BY user_id;

#SQL面试题很难吗?##SQL练习记录#
全部评论

相关推荐

小狗吃臭臭:以后用不到你设计的手机了,可惜!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务