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

连续签到领金币

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

思路是:求出签到的天数用dense_rank来进行排序,第二是用dense_rank来进行case when的签到,最后用临时表进行连接,

用临时表作为驱动表来sum()签到金币求和。得到

with a as(

select uid,

date(in_time) dt,

DENSE_RANK()over(partition by uid order by date(in_time)) bh

from tb_user_log

where date(in_time) between '2021-07-07' and '2021-11-01' and artical_id=0 and sign_in=1

),b as (

select *,

case DENSE_RANK()over(partition by date_sub(dt,INTERVAL bh day),uid order by dt)%7

when 3 then 3

when 0 then 7

else 1 end

mt_day

from a

)

select uid,DATE_FORMAT(dt,"%Y%m") as month

,sum(mt_day) coin

from b

group by uid,month

order by month,uid

全部评论

相关推荐

点赞 评论 收藏
分享
包行:平时怎么刷算法题的哇,字节的手撕听说都很难
字节跳动工作体验
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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