【牛客】SQL142 对试卷得分做min-max归一化

文章介绍了如何使用SQL查询对用户作答高难度试卷的得分进行min-max归一化,将范围缩放到[0,100]区间,并提供了两种方法:一是利用groupby聚合函数,二是使用窗口函数。最终输出包含用户ID、试卷ID和归一化后的平均分数,按试卷ID升序、归一化分数降序排列。
摘要由CSDN通过智能技术生成

描述

现有试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间):

idexam_idtagdifficultydurationrelease_time
19001SQLhard602020-01-01 10:00:00
29002C++hard802020-01-01 10:00:00
39003算法hard802020-01-01 10:00:00
49004PYTHONmedium702020-01-01 10:00:00

试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

iduidexam_idstart_timesubmit_timescore
6100390012020-01-02 12:01:012020-01-02 12:31:0168
9100190012020-01-02 10:01:012020-01-02 10:31:0189
1100190012020-01-01 09:01:012020-01-01 09:21:5990
12100290022021-05-05 18:01:01(NULL)(NULL)
3100490022020-01-01 12:01:012020-01-01 12:11:0160
2100390022020-01-01 19:01:012020-01-01 19:30:0175
7100190022020-01-02 12:01:012020-01-02 12:43:0181
10100290022020-01-01 12:11:012020-01-01 12:31:0183
4100390022020-01-01 12:01:012020-01-01 12:41:0190
5100290022020-01-02 19:01:012020-01-02 19:32:0090
11100290042021-09-06 12:01:01(NULL)(NULL)
8100190052020-01-02 12:11:01(NULL)(NULL)
在物理学及统计学数据计算时,有个概念叫min-max标准化,也被称为离差标准化,是对原始数据的线性变换,使结果值映射到[0 - 1]之间。转换函数为:

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

由示例数据结果输出如下:

uidexam_idavg_new_score
1001900198
100390010
1002900288
1003900275
1001900270
100490020

解释:高难度试卷有9001、9002、9003;

作答了9001的记录有3条,分数分别为68、89、90,按给定公式归一化后分数为:0、95、100,而后两个得分都是用户1001作答的,因此用户1001对试卷9001的新得分为(95+100)/2≈98(只保留整数部分),用户1003对于试卷9001的新得分为0。最后结果按照试卷ID升序、归一化分数降序输出。

方法一:使用group by聚合函数

#使用group by聚合函数
select
uid,exam_id,ifnull(round(avg((score-min_score)/differ_score)*100,0),min(score)) as avg_new_score
from
(
    select
    exam_id,min(score) as min_score,max(score)-min(score) as differ_score
    from
    exam_record
    where submit_time is not null and
    exam_id in (select exam_id from examination_info where difficulty='hard')
    group by exam_id
)t1
left join
(
    select uid,exam_id,score
    from
    exam_record
    where submit_time is not null and
    exam_id in (select exam_id from examination_info where difficulty='hard')
)t2
using(exam_id)
group by uid,exam_id
order by exam_id,avg_new_score desc

方法二:使用窗口函数

# 使用窗口函数
select
uid,exam_id,
round(avg(if(max_score=min_score,score,(score-min_score)/(max_score-min_score)*100)),0) as avg_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
    exam_record
    where submit_time is not null and
    exam_id in (select exam_id from examination_info where difficulty = 'hard')
)t1
group by uid,exam_id
order by exam_id,avg_new_score desc
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值