问题:

请你将用户作答高难度试卷的得分在每份试卷作答记录内执行min-max归一化后缩放到[0,100]区间,并输出用户ID、试卷ID、归一化后分数平均值;最后按照试卷ID升序、归一化分数降序输出。(注:得分区间默认为[0,100],如果某个试卷作答记录中只有一个得分,那么无需使用公式,归一化并缩放后分数仍为原分数)。

思路

1、求得每套试卷的最低分和最高分——聚合函数 2、然后用公式算每一行的min-max,注意如果某个试卷作答记录中只有一个得分,那么无需使用公式,说明要用if 3、分组,求平均值

select uid,exam_id,round(avg(new_score),0) avg_new_score
from(
    select uid,exam_id,
        if(max_score=min_score,score,100*(score-min_score)/(max_score-min_score)) new_score
    from
        (select uid,exam_id,score,
            max(score) over(partition by exam_id) as max_score,
            min(score) over(partition by exam_id) as min_score
        from examination_info join exam_record using(exam_id)
        where difficulty='hard') a
    ) b
where new_score is not null
group by exam_id,uid
order by exam_id,avg_new_score desc