题解 | #连续两次作答试卷的最大时间窗#

连续两次作答试卷的最大时间窗

https://www.nowcoder.com/practice/9dcc0eebb8394e79ada1d4d4e979d73c

with new as
(select
uid, exam_id, times, 
row_number() over (partition by uid order by times) rk
from
(select
uid, exam_id, date_format(start_time, '%Y-%m-%d') times
from 
exam_record
where year(start_time) = 2021
group by uid, exam_id, date_format(start_time, '%Y-%m-%d')
) t
)

select uid, days_window, avg_exam_cnt
from
(select
uid, max(df)+1 days_window,
round((max(df)+1) * (count(*) + 1) / (sum(df) +1), 2) avg_exam_cnt
from
(select
t1.uid, t1.exam_id, datediff(t2.times, t1.times) df
from 
new t1
inner join
new t2
on t1.rk = t2.rk-1 and t1.uid  = t2.uid
order by t1.uid
) t3
group by uid
) t4
where avg_exam_cnt <> 2.00
order by days_window desc, avg_exam_cnt desc

全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
11-15 18:39
已编辑
西安交通大学 Java
全村最靓的仔仔:卧槽,佬啥bg呢,本也是西交么
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务