题解 | #连续两次作答试卷的最大时间窗#
连续两次作答试卷的最大时间窗
https://www.nowcoder.com/practice/9dcc0eebb8394e79ada1d4d4e979d73c
-- 题意其实没有太说清楚,最大间隔天数是否+1,已经最大最小天数差是否加1,同时,做题如果重复做一道题,需要记入,不去重.
-- 逻辑不难,就是题意不明,全靠猜o(╥﹏╥)o
select
t.uid
# ,timestampdiff(day,t.start_time,le_start_time) days_window
,max(datediff(coalesce(le_start_time,t.start_time),t.start_time))+1 days_window
,round((max(datediff(coalesce(le_start_time,t.start_time),t.start_time))+1) *count(t1.exam_id)/(datediff(max(t.start_time),min(t.start_time))+1),2) avg_exam_cnt
from
(
select
uid
,id
,start_time
,lead(start_time,1)over(partition by uid order by start_time) le_start_time
from exam_record
where uid in
(
select
uid
from exam_record
where year(start_time)='2021'
group by uid
having count(distinct date(submit_time)) >=2
)
) t left join exam_record t1 on t.id=t1.id
where year(t.start_time)='2021'
group by t.uid
order by days_window desc ,avg_exam_cnt desc