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

连续签到领金币

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

with A as (
select distinct uid,date(in_time)date from tb_user_log
where date(in_time) between '2021-07-07' and '2021-10-31'
and sign_in = 1
and artical_id = 0
)
# 排序
,B as (
select uid
,date
,date - row_number()over(partition by uid order by date) d
,row_number()over(partition by uid order by date)rn
from A
)
# 总签到次数
,C as (
select uid,date_format(date,'%Y%m')month,count(*)coin
from B
group by 1,2
)
# 再排一次
,D as (
select uid
,d
,row_number()over(partition by uid,d order by d)rn
from B
)
# 分组取连续登录次数大于2的
,E as (
select uid,d,max(rn)rn from D group by 1,2 having rn>2
)
# 对上表结果细分 得额外可领金币数
,F as (
select uid,substr(d,1,6)month
,case when max(rn) between 3 and 6 then 2
when max(rn)=7 then 8
when max(rn)>7 and max(rn)%7<3 then floor(max(rn)/7)*8
when max(rn)>7 and max(rn)%7>2 then floor(max(rn)/7)*8 + 2
end coin
from E
group by 1,2
)
# 连表
select C.uid,C.month,C.coin + ifnull(F.coin,0) as coin from C
left join F using(uid)

全部评论
没什么难点就是***题目 要考察你细不细心 把这两个添加加上就行了sign_in = 1 and artical_id = 0
点赞 回复 分享
发布于 2022-12-15 10:56 浙江

相关推荐

牛仔知道哦:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
牛客156169118号:这怎么看是不是外包啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务