题解 | #SQL类别高难度试卷得分的截断平均值#
SQL类别高难度试卷得分的截断平均值
https://www.nowcoder.com/practice/a690f76a718242fd80757115d305be45
select (
select tag
from examination_info
where tag='sql'
limit 1
)
,(select difficulty
from examination_info
where tag='sql'
limit 1)
,round((sum(score)-max(score)-min(score))/(count(uid)-2),1) clip_avg_score
from exam_record
where exam_id=(select exam_id
from examination_info
where tag='sql' and difficulty='hard') and score is not null
这道题我第一个想法就是用子查询来做,但是一般来说子查询运行速度很慢,所以想着用表连接再做一遍,写了一遍,发现自己写的代码还是很复杂:
select tag
,difficulty
,clip_avg_score
from examination_info
left join (
select round((sum(score)-max(score)-min(score))/(count(exam_id)-2),1) clip_avg_score
,exam_id
from exam_record
where score is not null
group by exam_id) ex
using(exam_id)
where tag='sql' and difficulty='hard'
想和各位交流一下,该怎么简化自己的代码呢?现在看到题目虽然能做出来,但是感觉思维一直都不是很简洁,感谢各位
