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

官网链接:

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

0 问题描述

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

1 数据准备

sql 复制代码
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)

sql 复制代码
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

方式二:开窗函数

sql 复制代码
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进行计算。

相关推荐
倔强的石头_1 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou643 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1775 天前
《从零搭建NestJS项目》
数据库·typescript
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏5 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐5 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再5 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest5 天前
数据库SQL学习
数据库·sql