题解 | #连续签到领金币#

连续签到领金币

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

with zb_0 as (select *,CASE WHEN da >= 7 AND FLOOR(MOD(da,7) / 3) < 1 THEN FLOOR(da / 7) * 15 + MOD(da,7)  /*大于7天且余数不大于3*/
    WHEN da >= 7 AND FLOOR(MOD(da,7) / 3) >= 1 THEN FLOOR(da / 7) * 15 + 5 + MOD(da,7) - 3  /*大于7天且余数大于3*/
    WHEN da >= 3 THEN 5 + da - 3 /*大于三天*/
    ELSE da /*小于三天*/
END AS s
from 
(select uid,min(in_time) in_time,in_time-row_1 diff_time, /*求差,不同值个数就是各个连续签到天数*/
count(*) da
from 
(select uid,date(in_time) in_time,row_number() over(partition by uid order by in_time) row_1 
from tb_user_log
where artical_id=0 and sign_in=1 and date(in_time) BETWEEN '2021-07-07' and '2021-10-31') zb /*限定条件*/
group by uid,diff_time) zb1)
select uid,date_format(in_time,"%Y%m") month,sum(s) coin
from zb_0
group by uid,month
order by month,uid

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 19:05
点赞 评论 收藏
分享
头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务