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

连续签到领金币

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;
全部评论

相关推荐

11-05 18:26
门头沟学院 Java
点赞 评论 收藏
分享
10-07 23:57
已编辑
电子科技大学 Java
八街九陌:博士?客户端?开发?啊?
点赞 评论 收藏
分享
10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务