题解 | #第二快/慢用时之差大于试卷时长一半的试卷#

第二快/慢用时之差大于试卷时长一半的试卷

https://www.nowcoder.com/practice/b1e2864271c14b63b0df9fc08b559166

终于算是自己做出一道比较麻烦的题,这题其实不算特别难,主要就是理清逻辑,无限套娃,在这里分享一下自己的思路,虽然代码比较长,但绝对好理解。

第1步:先用from子查询+降序排序窗口函数,制作包含不同试卷ID及其对应被作答用时第二慢的所花的时间的表a
(select exam_id,desc_time,desc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) desc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) desc) desc_ranking from exam_record
where submit_time is not null) t1
where desc_ranking = 2) a
(注意要限制submit_time is not null,不然就会有null值进入所花时间的排序!!!
第2步:用跟第一步同样的方法,将降序改为升序,制作包含不同试卷ID及其对应被作答用时第二快的所花的时间的表b
(select exam_id,asc_time,asc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) asc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) asc) asc_ranking from exam_record
where submit_time is not null) t2
where asc_ranking = 2) b

第3步:将表a的exam_id和表b的exam_id作为连接键,将表a和表b连接起来,制作包含不同试卷ID及其对应第二快和第二慢用时之差的表c
(select a.exam_id exam_id,desc_time - asc_time time from
(select exam_id,desc_time,desc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) desc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) desc) desc_ranking from exam_record
where submit_time is not null) t1
where desc_ranking = 2) a
join 
(select exam_id,asc_time,asc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) asc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) asc) asc_ranking from exam_record
where submit_time is not null) t2
where asc_ranking = 2) b
on a.exam_id = b.exam_id) c

最后一步:将表c的exam_id和原表examination_infoexam_id作为连接键,将表c和表examination_info连接起来,添加限制条件后得到我们最终的运行代码
select c.exam_id,duration,release_time from 
(select a.exam_id exam_id,desc_time - asc_time time from
(select exam_id,desc_time,desc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) desc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) desc) desc_ranking from exam_record
where submit_time is not null) t1
where desc_ranking = 2) a
join 
(select exam_id,asc_time,asc_ranking from
(select exam_id,timestampdiff(second,start_time,submit_time) asc_time,row_number()over(partition by exam_id order by timestampdiff(second,start_time,submit_time) asc) asc_ranking from exam_record
where submit_time is not null) t2
where asc_ranking = 2) b
on a.exam_id = b.exam_id) c
left join examination_info on c.exam_id = examination_info.exam_id
where time > duration*60/2
order by c.exam_id desc



全部评论

相关推荐

06-20 15:23
门头沟学院 Java
难道你们背八股都不觉得累?现在每天背八股背的我想吐
想去大厂的土豆子:累不累都是对比出来的,八股可比高考、考研轻松多了
点赞 评论 收藏
分享
鼠鼠没有找到暑期实习,简历太空了,感觉直接去秋招会完蛋,这个时间点找个日常实习混个简历,边实习边准备秋招有没有搞头啊
梦想是成为七海千秋:可以的完全可以的,找不到暑期就找日常,秋招之前还是有很多时间可以实习的,哪怕只实习了一个月都可以写在简历上
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务