一、明确需求

计算所有用户完成SQL类别高难度试卷得分的截断平均值(去掉一个最大值和一个最小值后的平均值)

二、观察数据

两个表: examination_info:exam_id,tag,difficulty

exma_record:uid,exam_id,score 两张表都有我们想要的数据,故两个表需要联结,联结条件为exam_id。

三、做题思路

1、联结筛选数据+两次窗口函数排序

#两表联结,取SQL和难度为hard,同时排除null的数据
with t1 AS
(
select distinct a.*,b.tag,b.difficulty
from exam_record a 
    INNER JOIN examination_info b on a.exam_id = b.exam_id
where b.tag = 'SQL' AND b.difficulty = 'hard'
)

#以score降序、升序排列
select x.tag,x.difficulty,round(avg(x.score),1) clip_avg_score
from 
(select t1.*,row_number() over(partition by exam_id order by score DESC) h 
    , row_number() over(partition by exam_id order by score ) l
from t1 )x 
where x.h <> 1 and x.l <>1
group by x.tag,x.difficulty 

拓展:

1、round:保留位数的四舍五入函数 语法:round(col,<保留位数>) 2、null:聚合函数不会对null纳入计算