【牛客】SQL123 SQL类别高难度试卷得分的截断平均值

描述

牛客的运营同学想要查看大家在SQL类别中高难度试卷的得分情况。

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

示例数据:examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间)

|----|---------|-----|------------|----------|---------------------|
| id | exam_id | tag | difficulty | duration | release_time |
| 1 | 9001 | SQL | hard | 60 | 2020-01-01 10:00:00 |
| 2 | 9002 | 算法 | medium | 80 | 2020-08-02 10:00:00 |

示例数据:exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分)

|----|------|---------|---------------------|---------------------|--------|
| id | uid | exam_id | start_time | submit_time | score |
| 1 | 1001 | 9001 | 2020-01-02 09:01:01 | 2020-01-02 09:21:01 | 80 |
| 2 | 1001 | 9001 | 2021-05-02 10:01:01 | 2021-05-02 10:30:01 | 81 |
| 3 | 1001 | 9001 | 2021-06-02 19:01:01 | 2021-06-02 19:31:01 | 84 |
| 4 | 1001 | 9002 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 | 89 |
| 5 | 1001 | 9001 | 2021-09-02 12:01:01 | (NULL) | (NULL) |
| 6 | 1001 | 9002 | 2021-09-01 12:01:01 | (NULL) | (NULL) |
| 7 | 1002 | 9002 | 2021-02-02 19:01:01 | 2021-02-02 19:30:01 | 87 |
| 8 | 1002 | 9001 | 2021-05-05 18:01:01 | 2021-05-05 18:59:02 | 90 |
| 9 | 1003 | 9001 | 2021-09-07 12:01:01 | 2021-09-07 10:31:01 | 50 |
| 10 | 1004 | 9001 | 2021-09-06 10:01:01 | (NULL) | (NULL) |

根据输入你的查询结果如下:

|-----|------------|----------------|
| tag | difficulty | clip_avg_score |
| SQL | hard | 81.7 |

从examination_info表可知,试卷9001为高难度SQL试卷,该试卷被作答的得分有[80,81,84,90,50],去除最高分和最低分后为[80,81,84],平均分为81.6666667,保留一位小数后为81.7

输入描述:

输入数据中至少有3个有效分数

sql 复制代码
with cte as (
    select exam_id,score,tag,difficulty,
    dense_rank() over (partition by exam_id order by score) as rnk1,
    dense_rank() over (partition by exam_id order by score desc) as rnk2
    from 
    exam_record left join examination_info using (exam_id)
    where score is not null and tag = 'SQL'and difficulty = 'hard'
)

select tag,difficulty,
round(avg(score),1) as clip_avg_score
from cte
where score between 
(select score from cte where rnk1=2) 
and (select score from cte where rnk2=2)
group by tag,difficulty

或:

sql 复制代码
with cte as (
    select exam_id,score,tag,difficulty,
    dense_rank() over (partition by exam_id order by score) as rnk1,
    dense_rank() over (partition by exam_id order by score desc) as rnk2
    from 
    exam_record left join examination_info using (exam_id)
    where score is not null and tag = 'SQL'and difficulty = 'hard'
)

select tag,difficulty,
round(avg(score),1) as clip_avg_score
from cte
where rnk1!=1 and rnk2!=1
group by tag,difficulty
相关推荐
云飞云共享云桌面6 分钟前
1台电脑10个画图设计用怎么实现
linux·运维·服务器·网络·数据库·自动化·电脑
TTBIGDATA36 分钟前
【Ambari监控】Ambari-Metrics 的分支研究
大数据·数据库·hadoop·ambari·bigtop·edp·hidataplus
Z_z在努力1 小时前
【杂类】应对 MySQL 处理短时间高并发的请求:缓存预热
数据库·mysql·缓存
格林威1 小时前
Linux使用-MySQL的使用
linux·运维·人工智能·数码相机·mysql·计算机视觉·视觉检测
望获linux2 小时前
【实时Linux实战系列】规避缺页中断:mlock/hugetlb 与页面预热
java·linux·服务器·数据库·chrome·算法
longerxin20202 小时前
MongoDB 在线安装-一键安装脚本(CentOS 7.9)
数据库·mongodb·centos
水无痕simon2 小时前
3 水平分表
java·数据库
恣艺2 小时前
探索数据库世界:从基础类型到实际应用
数据库
小钻风33663 小时前
IDEA连接redis数据库时出现Failed to connect to any host resolved for DNS name.
数据库
ulias2123 小时前
单元最短路问题
数据库·c++·算法·动态规划