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

题目描述
https://www.nowcoder.com/practice/b1e2864271c14b63b0df9fc08b559166

思路:找到第二快和第二慢用时之差大于试卷时长的一半的试卷信息,按试卷ID降序排序。由示例数据结果输出如下:

先按照试卷进行排序,分别升序和降序取出第二块和第二慢

select r.exam_id,
nth_value(timestampdiff(second,r.start_time,r.submit_time),2) over (partition by r.exam_id order by timestampdiff(second,r.start_time,r.submit_time) desc) as slow_2,
nth_value(timestampdiff(second,r.start_time,r.submit_time),2) over (partition by r.exam_id order by timestampdiff(second,r.start_time,r.submit_time) asc) as fast_2,
e.duration,e.release_time
from exam_record as r join examination_info as e on r.exam_id=e.exam_id

运行结果

exam_idslow_2fast_2durationrelease_time
90013000None602021-09-01 06:00:00
90013000600602021-09-01 06:00:00
90013000600602021-09-01 06:00:00
90013000600602021-09-01 06:00:00
90013000600602021-09-01 06:00:00
9001None600602021-09-01 06:00:00
90021800None602021-09-01 06:00:00
900218001740602021-09-01 06:00:00
900218001740602021-09-01 06:00:00
900218001740602021-09-01 06:00:00
9002None1740602021-09-01 06:00:00
9003NoneNone802021-09-01 10:00:00

在对时间之差进行计算,记得要distinct

# 先按照试卷进行排序,分别升序和降序取出第二块和第二慢
select distinct exam_id,duration,release_time from(
    # 先按照试卷进行排序,分别升序和降序取出第二块和第二慢
    select r.exam_id,
    nth_value(timestampdiff(second,r.start_time,r.submit_time),2) over (partition by r.exam_id order by timestampdiff(second,r.start_time,r.submit_time) desc) as slow_2,
    nth_value(timestampdiff(second,r.start_time,r.submit_time),2) over (partition by r.exam_id order by timestampdiff(second,r.start_time,r.submit_time) asc) as fast_2,
    e.duration,e.release_time
    from exam_record as r join examination_info as e on r.exam_id=e.exam_id
) as t
where slow_2-fast_2>duration*30
order by exam_id desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值