牛客网SQL进阶123:高难度试卷的得分的截断平均值

  官网链接:

SQL类别高难度试卷得分的截断平均值_牛客题霸_牛客网牛客的运营同学想要查看大家在SQL类别中高难度试卷的得分情况。 请你帮她从exam_。题目来自【牛客题霸】icon-default.png?t=N7T8https://www.nowcoder.com/practice/a690f76a718242fd80757115d305be45?tpId=240&tqId=2180959&ru=%2Fpractice%2Ff6b4770f453d4163acc419e3d19e6746&qru=%2Fta%2Fsql-advanced%2Fquestion-ranking&sourceUrl=

0 问题描述

      从exam_record作答记录表中计算所有用户完成SQL类别高难度试卷得分的截断平均值(去掉一个最大值和一个最小值后的平均值)。

1 数据准备

drop table if exists examination_info;
CREATE TABLE  examination_info (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
    tag varchar(32) COMMENT '类别标签',
    difficulty varchar(8) COMMENT '难度',
    duration int NOT NULL COMMENT '时长',
    release_time datetime COMMENT '发布时间'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

drop table if exists exam_record;
CREATE TABLE exam_record (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid int NOT NULL COMMENT '用户ID',
    exam_id int NOT NULL COMMENT '试卷ID',
    start_time datetime NOT NULL COMMENT '开始时间',
    submit_time datetime COMMENT '提交时间',
    score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  (9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
  (9002, '算法', 'medium', 80, '2020-08-02 10:00:00');

INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-02 09:01:01', '2020-01-02 09:21:01', 80),
(1001, 9001, '2021-05-02 10:01:01', '2021-05-02 10:30:01', 81),
(1001, 9001, '2021-06-02 19:01:01', '2021-06-02 19:31:01', 84),
(1001, 9002, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 89),
(1001, 9001, '2021-09-02 12:01:01', null, null),
(1001, 9002, '2021-09-01 12:01:01', null, null),
(1002, 9002, '2021-02-02 19:01:01', '2021-02-02 19:30:01', 87),
(1002, 9001, '2021-05-05 18:01:01', '2021-05-05 18:59:02', 90),
(1003, 9001, '2021-02-06 12:01:01', null, null),
(1003, 9001, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 50);

2 数据分析

方式一:截断平均值:(总和-最大值-最小值) / (总个数-2) = (sum(score) - max(score) - min(score)) / (count(score) - 2)

select tag,
       difficulty,
       -- 平均值保留1位小数
       round((sum(score) - max(score) - min(score)) / (count(score) - 2), 1) as clip_avg_score
from exam_record er
join examination_info ei on er.exam_id = ei.exam_id
where ei.tag="SQL" and ei.difficulty="hard"

需要注意:

  • 聚合函数sum, max ,min 会自定忽略null值
  • 重复的最大值/最小值,该题只用去掉一个最大值/最小值就行,所以分母是count(socre) -2 

方式二:开窗函数

select  tag,
        difficulty,
        round(avg(score), 1) as clip_avg_score
from(select  tag,
             difficulty,
             score,
             --开窗
             row_number() over(order by score asc) as rk_asc,
             row_number() over(order by score desc) as rk_desc
   from examination_info as i
   join exam_record as r
   on i.exam_id = r.exam_id
   where tag = 'SQL' and difficulty = 'hard' and score is not null) as tmp
where rk_asc <> 1 and rk_desc <> 1
group by tag, difficulty;

需要注意:

  • 窗口函数不像max()和min()会自动忽略null值,所以要在where子句提前把null值去掉。否则row_number也会对它们排序

3 小结

    本案例中需要注意细节:除了 count(*)会计算null值,其余的聚合函数 sum(*), avg(*),max(*),min(*),count(1) 等中会自动忽略null值;但是开窗函数row_number() over、dense_rank() over、rank() over会对null进行计算。

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值