with t as (select *,timestampdiff(second,start_time,submit_time) time,row_number() over(partition by exam_id order by timestampdiff(second,start_time,submit_time)) rank1 ,row_number() over(partition by exam_id order by timestampdiff(second,start_time,submit_time) desc) rank2 from exam_record where ...