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

连续签到领金币

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

思路(执果索因)

累积金币数→每日金币数→连续签到天数→签到日期

  1. 累积金币数→每日金币数(sum()
  2. 每日金币数→连续签到天数(case when end) 金币规则:每七天为一周期
  • 第三天——连续天数%7=3 coin=3
  • 第七天——连续天数%7=0 coin=7
  • 其他——coin=1
  1. 连续签到天数→签到日期
  • 排序: 先把每个uid的签到日期按顺序排列,得到rn1
  • 怎么判断连续——分组: 若某几个日期是连续的,那么日期-rn1应是同一天,不妨记这一天为参考日期(ref_date),并以此分组,同一组的就是连续的日期。
  • 连续第几天——组内排序: 每组在组内排序rn2即为连续天数 综上,需要依次得到①uid,②签到日期sign_date,③每个uid总体日期排序rn1,④参考日期ref_date,⑤连续天数rn2,⑥当天金币数coin_num,⑦总金币数coin

代码

  1. 取变量①uid,②签到日期sign_date,③每个id总体日期排序rn1,得到q1(注意“article_id=0”时sign_in值才有效+2021.7.7-2021.10.31
select distinct uid,date(in_time) as sign_date,
 row_number() over (partition by uid 
                    order by date(in_time) ) as rn1
 from tb_user_log
 where in_time between '2021-07-07 00:00:00' and '2021-10-31 23:59:59'
 and sign_in=1 and artical_id=0
 order by uid,sign_date

alt

  1. 取变量④参考日期ref_date,⑤连续天数rn2,⑥当天金币数coin_num,得到q2(实际代码中④⑤⑥是依次嵌套的,只取⑥即可,这里为了解释得更清楚,把几个量都展示出来)
with q1 as
(select distinct uid,date(in_time) as sign_date,
 row_number() over (partition by uid 
                    order by date(in_time) ) as rn1
 from tb_user_log
 where in_time between '2021-07-07 00:00:00' and '2021-10-31 23:59:59'
 and sign_in=1 and artical_id=0
 order by uid,sign_date
)
select uid,sign_date,rn1,
 date_sub(sign_date,interval rn1 day) as ref_date,
 row_number() over (partition by uid,date_sub(sign_date,interval rn1 day) 
                    order by sign_date) as rn2,
case row_number() over (partition by uid,date_sub(sign_date,interval rn1 day) 
                    order by sign_date)%7
    when 3 then 3
    when 0 then 7
    else 1 end as coin_num
    from q1

alt

  1. 对变量⑥coin_num求和得到⑦coin
with q1 as
(select distinct uid,date(in_time) as sign_date,
 row_number() over (partition by uid 
                    order by date(in_time) ) as rn1
 from tb_user_log
 where in_time between '2021-07-07 00:00:00' and '2021-10-31 23:59:59'
 and sign_in=1 and artical_id=0
 order by uid,sign_date
),
q2 as 
(select uid,sign_date,rn1,
 date_sub(sign_date,interval rn1 day) as ref_date,
 row_number() over (partition by uid,date_sub(sign_date,interval rn1 day) 
                    order by sign_date) as rn2,
case row_number() over (partition by uid,date_sub(sign_date,interval rn1 day) 
                    order by sign_date)%7
    when 3 then 3
    when 0 then 7
    else 1 end as coin_num
    from q1)        
  
select uid,date_format(sign_date,'%Y%m') as month,sum(coin_num) as coin
from q2
group by uid,month
order by month,uid

alt

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-29 22:21
Offer1:小马智行,深圳,测试开发工程师,17.0k*16.0,Offer2:追觅科技,深圳,嵌入式工程师,18.0k*15.0,
嵌软狗都不学:各位base深圳的同事,作为也是并肩作战的一员,今天想站在管理视角,和大家开诚布公地聊一聊:从近几个月的上下班数据对比看来,我们发现一个明显的差异:深圳同事的在岗时间普遍比苏州同事短。很多深圳同事早上9点之后才到公司,晚上不到 20 点就下班了;而总部那边,20点半甚至 22 点后还有不少同事在办公室忙碌,特别是研发团队,加班更是常态。相信去过苏州的同事,对这种场景都不陌生。我很好奇,这是因为苏州工作任务太重还是咱们深圳同事效率真的高到能在更短时间内完成工作?MOVA在深圳成立分公司是为了吸引更优秀的人才贡献更多更高质的价值,公司管理层给我反馈的是深圳招到的多是行业的专家大拿,大部分都是薪资比苏州高的,而且我们办公的租金等也远高于苏州的..MOVA虽脱胎于强壮的集团母体不久,各业务板块尚未实现全面盈利,虽说公司管理层目光长远,不纠结当下的人才投入,但行业内的普遍标准是,员工创造的价值要达到公司雇佣成本的 15 倍以上。大家不妨自我审视一下,自己是否达到了这个标准?如果是抱着划水、按时打卡走人拿毛爷爷的心态那不适合来MOVA,那样过下去不但自己过得尴尬也会影响MOVA这个大船的攻城略地的速度.我并非鼓励大家盲目加班,而是倡导高效工作,拒绝无效忙碌,不要让项目进度因低效受影响,也别把精力浪费在和苏州同事拼打卡时长上,提倡更高的人效比;考虑到两地地域和交通差异,相信大家会找最适合自己发挥的工作方式(比如按时下班后1小时到家晚饭后继续未竟工作等..)大家在遵守公司规章的情况下尽情地体现自己的能力价值,为MOV!和深圳公司争光我们在这边才能更安心更有信心的工作下去;请客BU长、名部门长、项目管理和各业务单元负责人,全面梳理团队情况,及时评估成员工作负荷与成果质量,坚决清退划水害虫痕疫,践行公司价值观,相互监督,防止管理漏洞及渎职。感谢人家的理解,也请人家多担待我的直言不讳……
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
04-28 22:33
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务