题解 | #计算用户8月每天的练题数量#
连续签到领金币
http://www.nowcoder.com/practice/aef5adcef574468c82659e8911bb297f
with t1 as( -- t1为满足签到条件的用户以及其签到日期情况 select distinct uid, date(in_time) as dt, row_number() over( partition by uid order by date(in_time) ) as rnk from tb_user_log where date(in_time) between '2021-07-07' and '2021-10-31' and artical_id = 0 and sign_in = 1 ), t2 as( -- t2在t1的基础上获得dt_temp字段,用来判断是否为连续签到 select uid, dt, date_sub(dt, interval rnk day) as dt_temp from t1 ), t3 as( -- t3在t2的基础上确定该天是该用户的第几天连续签到 select uid, dt, row_number() over( partition by uid, dt_temp order by dt ) as lx_day from t2 ) select uid, date_format(dt, '%Y%m') as month, sum(cnt) as coin from ( -- 获得用户在每天的签到金币数 select uid, dt, case lx_day % 7 when 3 then 3 when 0 then 7 else 1 end as cnt from t3 ) t group by uid, date_format(dt, '%Y%m') order by month, uid