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

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

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

注意:rank()过不了,要用row_number()。
因为rank()表示排名数,会出现并列情况,如两个人并列第二的情况,算第二快/慢用时之差time_diff时,会把并列第二的两个人都算进去。

sum(case when rank_slow=2 then t
                           when rank_fast=2 then -t else 0 end
                          ) as time_diff

全部代码:

select  exam_id,duration,release_time
from (
        select a.exam_id,a.release_time,sum(case when rank_slow=2 then t 
                           when rank_fast=2 then -t else 0 end
                          ) as time_diff,a.duration
        from examination_info a
        left join (
                    select exam_id
                            ,timestampdiff(second,start_time,submit_time) as t
                            ,row_number() over(partition by exam_id 
                             order by timestampdiff(second,start_time,submit_time) desc
                            ) as rank_slow
                            ,row_number() over(partition by exam_id
                            order by timestampdiff(second,start_time,submit_time)
                            ) as rank_fast
                    from exam_record
                    where submit_time is not null
                    ) b 
        on a.exam_id=b.exam_id
        -- where rank_slow=2 and rank_fast=2
        group by a.exam_id
) aa
where time_diff>duration/2*60
order by exam_id desc
全部评论

相关推荐

02-12 00:59
已编辑
哈尔滨工业大学 产品经理
华为 软件开发岗 20.6*16薪 本科
点赞 评论 收藏
分享
头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务