题解 | 连续签到领金币

连续签到领金币

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

with
    t1 as (
        select distinct
            uid,
            date (in_time) as dt,
            dense_rank() over (
                partition by
                    uid
                order by
                    date (in_time)
            ) as dt1
        from
            tb_user_log
        where
            sign_in = 1
            and date (in_time) >= 20210707
            and date (in_time) < 20211101
            and artical_id=0
    )
select
    uid,
    date_format (dt, '%Y%m') as month,
    sum(
        CASE
            WHEN MOD(ddt, 7) = 3 THEN 3 -- 使用标准MOD函数替代%
            WHEN MOD(ddt, 7) = 0 THEN 7 -- 避免%符号导致的解析错误
            ELSE 1 -- 必须指定ELSE默认值
        END
    ) AS coin
from
    (
        select
            uid,
            dt,
            row_number() over (
                partition by
                    uid,
                    date_sub(dt, interval dt1 day)
            ) as ddt
        from
            t1
    ) tt
group by
    uid,
    month
    order by month ,uid

全部评论

相关推荐

牛客44320985...:你的当务之急是把这个糖的要死的沟槽ide主题改了
点赞 评论 收藏
分享
肖先生~:大一点得到公司面试更能学到点东西
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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