step1: 查找已签到的相关数据,按用户id进行分组,按签到日期排序后,对其依次编号;
step2: 将日期减去编号后得到dif,按照user_id及dif进行分组,统计个数即为每个用户连续签到的天数;
step3: 查找连续签到天数中的最大值;
step4: 按user_id排序输出。
SELECT t1.user_id, max(t1.max_continuous_days) as max_continuous_days
from(
select t.user_id, date_sub(t.sign_date, interval t.rn day) as dif, count(1) as max_continuous_days
from(
select user_id, sign_date, row_number() over(partition by user_id order by sign_date ) as rn
from user_sign_tb
where if_sign=1)t
group by t.user_id, dif)t1
group by t1.user_id
ORDER by t1.user_id;