题解 | #连续签到领金币#
连续签到领金币
https://www.nowcoder.com/practice/aef5adcef574468c82659e8911bb297f
-- 思路:
- -- 1.筛选出各用户符合条件的签到日期,得到<签到表>:求出各用户连续登录的天数
- -- 2.求出用户每日签到领取的金币数
- -- 3.按照uid,月份分组,求出对应的coin和。并且按照月份、id升序排序
补充:
-- 金币领取规则:这里,按照连续签到天数7的余数来处理即可
-- 当签到天数%7=3 则领取3金币
-- 当签到天数%7=0 则领取7金币
-- 其余情况,领取1金币
-- 思路: -- 1.筛选出各用户符合条件的签到日期,得到<签到表>:求出各用户连续登录的天数 -- 2.求出用户每日签到领取的金币数 -- 3.按照uid,月份分组,求出对应的coin和。并且按照月份、id升序排序 with t2 as ( -- 1.筛选出各用户符合条件的签到日期,得到<签到表>:求出各用户连续登录的天数。 select uid, dt, group_diff_dt, row_number() over(partition by uid, group_diff_dt order by dt) as con_days from ( select distinct uid, date(in_time) as dt, date(in_time) - (row_number() over(partition by uid order by date(in_time))) as group_diff_dt from tb_user_log where date(in_time) between '2021-07-07' and '2021-10-31' and artical_id = 0 and sign_in = 1 ) t1 ), t3 as ( -- 金币领取规则:这里,按照连续签到天数7的余数来处理即可 -- 当签到天数%7=3 则领取3金币 -- 当签到天数%7=0 则领取7金币 -- 其余情况,领取1金币 -- 2.求出用户每日签到领取的金币数 select uid, dt, group_diff_dt, case when con_days % 7 = 3 then 3 when con_days % 7 = 0 then 7 else 1 end as coin from t2 ) -- 3.按照uid,月份分组,求出对应的coin和。并且按照月份、id升序排序 select uid, date_format(dt, '%Y%m') as month, sum(coin) as coin from t3 group by uid, date_format(dt, '%Y%m') order by uid, date_format(dt, '%Y%m')
SQL大厂面试题 文章被收录于专栏
牛客网sql大厂面试题题解~