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

连续签到领金币

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

  • 难点:连续n次的筛选判定
    • 解决思路:可以参考计算连续出现3次的所有数字
    • 构建自增序列,与原序列相减得到序列差。
    • 序列差相同的意味着序列连续,序列差不同则意味着序列不是连续
select uid,
       month,
       sum(coin) as coin
from (
         select uid,
                month,
                min_date_diff - date_id                                                          as id_diff,
                count(*) div 7 * 15 + if(count(*) mod 7 > 2, count(*) mod 7 + 2, count(*) mod 7) as coin
         from (
                  select uid,
                         date_format(in_time, '%Y%m')                                                        as month,
                         row_number() over (partition by uid, month(in_time) order by in_time) as date_id,
                         datediff(in_time, min(in_time) over (partition by uid, month(in_time) order by in_time)) +
                         1                                                                     as min_date_diff
                  from tb_user_log
                  where artical_id = 0
                    and sign_in = 1
                    and in_time between '2021-07-07' and '2021-11-01'
              ) as t
         group by uid, month, min_date_diff - date_id
     ) as u
group by uid, month
order by month, uid;
全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
牛客84809583...:举报了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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