with t as (select uid,start_time,
lag(start_time,1)over(partition by uid order by start_time desc) as next_time
from exam_record
where year(start_time) = 2021)

select uid,max(datediff(next_time,start_time))+1 as days_window,
round(count(uid)/(datediff(max(start_time),min(start_time))+1)*(max(datediff(next_time,start_time))+1),2) as avg_exam_cnt from t 
group by uid
having datediff(max(start_time),min(start_time)) >= 1
order by days_window desc,avg_exam_cnt desc