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

连续签到领金币

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

【解题思路】

根据要求,直接思路应该是按照【用户和月份】进行分组求和即可,解题难点是连续签到的第3天和第6天可以额外领取金币,也就是其它天签到可以领取1枚金币,但是连续签到的第3天和第6天可以领取3枚和7枚金币。理想状态下,如果知道对应的签到日期是用户连续第几天签到,【再使用连续签到的天数%7得余数就可以了,当余数是3和0的时候,金币数量就是3和7,其它为1】。

【经典问题】问题:给你一堆日期,如何进行连续识别?

对一组日期进行排序编号,然后用日期减去(或者加上)编号【date_sub/date_add(date,interval rk_num day)】,若日期是连续的,那么做减法或者加法得到的结果一样。

因此,该问题的解题步骤如下:

第一步:对用户分组,然后对日期进行排序。

select  
    uid 
    ,in_time
    ,row_number() over (partition by uid order by date(in_time)) as rk_num
from tb_user_log
where artical_id=0 and sign_in=1 and date(in_time) between'2021-07-07' and  '2021-10-31'


----
+-----+---------------------+--------+
| uid | in_time             | rk_num |
+-----+---------------------+--------+
| 101 | 2021-07-07 10:00:00 |      1 |
| 101 | 2021-07-08 10:00:00 |      2 |
| 101 | 2021-07-09 10:00:00 |      3 |
| 101 | 2021-07-10 10:00:00 |      4 |
| 101 | 2021-07-11 23:59:55 |      5 |
| 101 | 2021-07-12 10:00:28 |      6 |
| 101 | 2021-07-13 10:00:28 |      7 |
| 101 | 2021-07-14 23:59:55 |      8 |
| 101 | 2021-07-15 10:00:28 |      9 |
| 101 | 2021-07-16 10:00:28 |     10 |
| 102 | 2021-10-01 10:00:28 |      1 |
| 102 | 2021-10-02 10:00:01 |      2 |
| 102 | 2021-10-03 23:59:55 |      3 |
| 102 | 2021-10-05 11:00:53 |      4 |
| 102 | 2021-10-06 11:00:45 |      5 |
+-----+---------------------+--------+
15 rows in set (0.01 sec)

第二步:获得登入日期与rk_num的差。

select  
    uid 
    ,date(in_time)
    ,row_number() over (partition by uid order by date(in_time)) as rk_num
    ,date_sub(date(in_time),interval row_number() over (partition by uid order by date(in_time)) day) as sign_rk
from tb_user_log
where artical_id=0 and sign_in=1 and date(in_time) between'2021-07-07' and  '2021-10-31'
;

----
+-----+---------------+--------+------------+
| uid | date(in_time) | rk_num | sign_rk    |
+-----+---------------+--------+------------+
| 101 | 2021-07-07    |      1 | 2021-07-06 |
| 101 | 2021-07-08    |      2 | 2021-07-06 |
| 101 | 2021-07-09    |      3 | 2021-07-06 |
| 101 | 2021-07-10    |      4 | 2021-07-06 |
| 101 | 2021-07-11    |      5 | 2021-07-06 |
| 101 | 2021-07-12    |      6 | 2021-07-06 |
| 101 | 2021-07-13    |      7 | 2021-07-06 |
| 101 | 2021-07-14    |      8 | 2021-07-06 |
| 101 | 2021-07-15    |      9 | 2021-07-06 |
| 101 | 2021-07-16    |     10 | 2021-07-06 |
| 102 | 2021-10-01    |      1 | 2021-09-30 |
| 102 | 2021-10-02    |      2 | 2021-09-30 |
| 102 | 2021-10-03    |      3 | 2021-09-30 |
| 102 | 2021-10-05    |      4 | 2021-10-01 |
| 102 | 2021-10-06    |      5 | 2021-10-01 |
+-----+---------------+--------+------------+
15 rows in set (0.00 sec)

第三步:若是日期连续,那么sign_rk就是相同的,此时再对sign_rk进行排序编号,就知道是连续签到的第几天了。

select
	uid
	,date_format(dt,'%Y-%m') as dt
    ,row_number() over(partition by uid,sign_rk order by dt) as conti_rk
from    
(select  
    uid 
    ,date(in_time) as dt
    ,row_number() over (partition by uid order by date(in_time)) as rk_num
    ,date_sub(date(in_time),interval row_number() over (partition by uid order by date(in_time)) day) as sign_rk
from tb_user_log
where artical_id=0 and sign_in=1 and date(in_time) between'2021-07-07' and  '2021-10-31'
) t1
;


----
+-----+---------+----------+
| uid | dt      | conti_rk |
+-----+---------+----------+
| 101 | 2021-07 |        1 |
| 101 | 2021-07 |        2 |
| 101 | 2021-07 |        3 |
| 101 | 2021-07 |        4 |
| 101 | 2021-07 |        5 |
| 101 | 2021-07 |        6 |
| 101 | 2021-07 |        7 |
| 101 | 2021-07 |        8 |
| 101 | 2021-07 |        9 |
| 101 | 2021-07 |       10 |
| 102 | 2021-10 |        1 |
| 102 | 2021-10 |        2 |
| 102 | 2021-10 |        3 |
| 102 | 2021-10 |        1 |
| 102 | 2021-10 |        2 |
+-----+---------+----------+
15 rows in set (0.00 sec)

第四步,分组取模出结果。最终代码如下:

select
	uid
	,dt as month
	,sum(case when conti_rk % 7 = 3 then 3 when conti_rk % 7 = 0 then 7 else 1 end) as coin
from	
    (select
        uid
        ,date_format(dt,'%Y%m') as dt
        ,row_number() over(partition by uid,sign_rk order by dt) as conti_rk
    from    
        (select  
            uid 
            ,date(in_time) as dt
            ,row_number() over (partition by uid order by date(in_time)) as rk_num
            ,date_sub(date(in_time),interval row_number() over (partition by uid order by date(in_time)) day) as sign_rk
        from tb_user_log
        where artical_id=0 and sign_in=1 and  date(in_time) between'2021-07-07' and  '2021-10-31'
        ) t1
    ) t2
group by uid,dt
order by uid,dt
;   

-- 注意:这里日期使用in_time >= '2021-07-01' and in_time <= '2021-10-31'有的实例过不了!!!
#SQL练习记录#
全部评论

相关推荐

03-27 16:40
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网计算机类笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功----------分割线-----------2026.3.21&nbsp;电网面试结束,感觉回答的还勉勉强强,大概是2个岗位分别招1个人,一共11人面试,实际来了9人2026.3.27&nbsp;出面试成绩,满分100分,早上10:20左右发现面试成绩46,我震惊了,没截图,后面过了十分钟重新看发现面试成绩给我改成58了。但同样震惊。朋友问我是不是把面试官打了,哈哈
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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