SQL进阶day12——空值处理

1 统计有未完成状态的试卷的未完成数和未完成率

(复习if的用法)

我的思路:

复制代码
select exam_idm
count(if submit_time is NULL then 1 else 0 end) incomplete_cnt,
count(if submit_time is NULL then 1 else 0 end)/count(submit_time) complete_rate
from exam_record
group by id

报错好像是因为(if submit_time is NULL then 1 else 0 end)不对。

查询改正:

复制代码
select exam_id,
sum(if (submit_time is NULL,1,0)) incomplete_cnt,
count(if (submit_time is NULL,1,0))/count(submit_time) complete_rate
from exam_record
group by id

正确答案:

复制代码
select exam_id,
sum(if (submit_time is NULL,1,0)) incomplete_cnt,
round(avg(if (submit_time is NULL,1,0)),3) complete_rate
from exam_record
group by exam_id
having complete_rate != 0

复盘:

(1)【条件判断函数】------根据满足不同条件,执行相应流程(用法不要搞混啦!)
● if(expr,v1,v2)

如果表达式expr是true返回值v1,否则返回v2

例如:if(1<2,'Y','N')返回Y,if(1>2,'Y','N')返回N if后面要加括号!!!!
● case when

case expr when v1 then r1 [when v2 then r2] ...[else rn] end

例如:case 2 when 1 then 'one' when 2 then 'two' else 'more' end 返回two

case后面的值为2,与第二条分支语句when后面的值相等相等,因此返回two

(2)是按照exam_id进行分组,统计有未完成状态的试卷的,是基于试卷来的。

2 0级用户高难度试卷的平均用时和平均得分

我的代码:

复制代码
#2、再筛选出试卷------difficulty = hard
select uid,
round(avg((if(score is NULL,0,score))),0) avg_score,
avg(if(submit_time is NULL,ei.duration,timestampdiff(minute,submit_time,start_time))) avg_time_took
from exam_record er join examination_info ei
on er.exam_id = ei.exam_id
where uid in  #1、先筛选出用户------level=0
(select uid from user_info
where level = 0)
and ei.difficulty = 'hard'
group by er.uid,er.exam_id
# 未完成的默认试卷最大考试时长
# if(submit_time is NULL,ei.duration,timestampdiff(minute,submit_time,start_time))

# 未完成的默认0分处理
# if(score is NULL,0,score)

答案错误原因:

复制代码
# 未完成的默认试卷最大考试时长
# if(submit_time is NULL,ei.duration,timestampdiff(minute,submit_time,start_time))

改正:

时间差函数:timestampdiff,如计算差多少分钟

timestampdiff(minute,时间1,时间2),是时间2-时间1。

复制代码
# 未完成的默认试卷最大考试时长
# if(submit_time is NULL,ei.duration,timestampdiff(minute,start_time,submit_time))

题目是要求:请输出每个0级用户所有的(..)试卷,所以是直接按照用户分组就好,

不要group by er.uid,er.exam_id

正确答案:

复制代码
#2、再筛选出试卷------difficulty = hard
select uid,
round(avg((if(score is NULL,0,score))),0) avg_score,
round(avg(if(submit_time is NULL,ei.duration,timestampdiff(minute,start_time,submit_time))),1) avg_time_took
from exam_record er join examination_info ei
on er.exam_id = ei.exam_id
where uid in  #1、先筛选出用户------level=0
(select uid from user_info
where level = 0)
and ei.difficulty = 'hard'
group by er.uid
# 未完成的默认试卷最大考试时长
# if(submit_time is NULL,ei.duration,timestampdiff(minute,start_time,submit_time))

# 未完成的默认0分处理
# if(score is NULL,0,score)
相关推荐
码界筑梦坊4 分钟前
173-基于Flask的微博舆情数据分析系统
后端·python·数据分析·flask·毕业设计
叁沐28 分钟前
MySQL 28 读写分离有哪些坑?
mysql
nightunderblackcat1 小时前
新手向:异步编程入门asyncio最佳实践
前端·数据库·python
DarkAthena1 小时前
【GaussDB】使用MySQL客户端连接到GaussDB的M-Compatibility数据库
数据库·mysql·gaussdb
livemetee1 小时前
Flink2.0学习笔记:使用HikariCP 自定义sink实现数据库连接池化
大数据·数据库·笔记·学习·flink
人大博士的交易之路1 小时前
龙虎榜——20250822
大数据·数据挖掘·数据分析·缠中说禅·龙虎榜·道琼斯结构
XXD啊2 小时前
Redis 从入门到实践:Python操作指南与核心概念解析
数据库·redis·python
好望角雾眠5 小时前
第三阶段数据库-7:sql中函数,运算符,常用关键字
数据库·笔记·sql·学习·sqlserver·c#
牛角上的男孩7 小时前
apt update Ign and 404 Not Found
开发语言·数据库
帧栈10 小时前
开发避坑指南(29):微信昵称特殊字符存储异常修复方案
java·mysql