select id as "考试编号"
,name as "姓名"
,module1 as "模块1考试成绩"
,module2 as "模块2考试成绩"
,case when module1>=60 and module2>=60 then '考试通过' else '考试未通过' end as "考试结果"
from exam a
-- 如果有通过,则把没通过的记录都过滤掉
where not exists (
select 1 from exam
where name=a.name
and (case when module1>=60 and module2>=60 then 1 else 0 end)>(case when a.module1>=60 and a.module2>=60 then 1 else 0 end)
)
-- 保留通过和没通过各自的最高分
and not exists (
select 1 from exam
where name=a.name
and (case when module1>=60 and module2>=60 then 1 else 0 end)=(case when a.module1>=60 and a.module2>=60 then 1 else 0 end)
and (module1+module2)>(a.module1+a.module2)
)