我的解题内容:
with t as( select a.emp_id,a.exam_id,b.emp_level,case when (a.exam_id=9001 and score>78.667 and round(timestampdiff(second,start_time,submit_time)/60,1) < 27.3) then a.emp_id else null end 9001isgood, case when (a.exam_id=9002 and score>75.833 and round(timestampdiff(second,start_time,submit_time)/60,1) < 34.2) then a.emp_id else null end 9002isgood from exam_record a left join emp_info b on a.emp_id =b.emp_id where b.emp_level < 7 ) select t.emp_id,emp_level,c.tag from t left join examination_info c on c.exam_id=t.exam_id where 9001isgood is not null or 9002isgood is not null
运用了窗口函数的便捷:通过窗口函数动态计算 每个考试(所有 exam_id) 的平均分数和平均耗时,然后通用化判断员工在对应考试中是否 “分数高于该考试平均分、耗时低于该考试平均耗时”,且员工级别 < 7。
with t as (select *, round(avg(score) over (partition by exam_id),3) as examavgscore, round(avg(timestampdiff(second,start_time,submit_time)/60) over (partition by exam_id),1) as avgtime from exam_record ) select t.emp_id,emp_level,tag from t left join emp_info a on a.emp_id=t.emp_id left join examination_info b on b.exam_id=t.exam_id where emp_level<7 and score>examavgscore and round(timestampdiff(second,start_time,submit_time)/60,1)<avgtime order by t.emp_id

京公网安备 11010502036488号