题解 | 连续签到领金币

连续签到领金币

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



select-- 按月计算金币数
    uid
    ,date_format(dt,'%Y%m') 'month'
    ,sum(case 
            when n=3 then 3
            when n=0 then 7
            else 1 end) as gold_num 
from
(-- 重置天数
    select
        uid
        ,dt 
        ,sign_day%7 as n 
    from
    (  -- 判断是连续的第几天
        select
            uid
            ,dt 
            , row_number()over(partition by t111.uid,t111.dt_sub order by t111.dt) as sign_day 
        from
        (   -- 判断连续 
            select
                uid
                ,dt
                ,date_sub(dt, interval dr day) as dt_sub -- 计算日期和排序差值
            from
            ( -- 日期排序
                select 
                    uid
                    , date(in_time) dt
                    , row_number()over(partition by uid order by date(in_time)) as dr
                from tb_user_log
                where in_time between '2021-07-07 00:00:00' and '2021-11-1 00:00:00' 
                and artical_id=0 and sign_in=1
            ) t1111
            group by 
                uid
                ,dt
                ,date_sub(dt, interval dr day) 
        ) t111
    ) t11
) t1 
group by 
    uid
    ,date_format(dt,'%Y%m')
order by
    uid
    ,date_format(dt,'%Y%m')

全部评论

相关推荐

对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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