• SQL进阶day12——空值处理


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

    (复习if的用法)

    我的思路:

    1. select exam_idm
    2. count(if submit_time is NULL then 1 else 0 end) incomplete_cnt,
    3. count(if submit_time is NULL then 1 else 0 end)/count(submit_time) complete_rate
    4. from exam_record
    5. group by id

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

    查询改正:

    1. select exam_id,
    2. sum(if (submit_time is NULL,1,0)) incomplete_cnt,
    3. count(if (submit_time is NULL,1,0))/count(submit_time) complete_rate
    4. from exam_record
    5. group by id

    正确答案:

    1. select exam_id,
    2. sum(if (submit_time is NULL,1,0)) incomplete_cnt,
    3. round(avg(if (submit_time is NULL,1,0)),3) complete_rate
    4. from exam_record
    5. group by exam_id
    6. 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级用户高难度试卷的平均用时和平均得分

    我的代码:

    1. #2、再筛选出试卷——difficulty = hard
    2. select uid,
    3. round(avg((if(score is NULL,0,score))),0) avg_score,
    4. avg(if(submit_time is NULL,ei.duration,timestampdiff(minute,submit_time,start_time))) avg_time_took
    5. from exam_record er join examination_info ei
    6. on er.exam_id = ei.exam_id
    7. where uid in #1、先筛选出用户——level=0
    8. (select uid from user_info
    9. where level = 0)
    10. and ei.difficulty = 'hard'
    11. group by er.uid,er.exam_id
    12. # 未完成的默认试卷最大考试时长
    13. # if(submit_time is NULL,ei.duration,timestampdiff(minute,submit_time,start_time))
    14. # 未完成的默认0分处理
    15. # if(score is NULL,0,score)

    答案错误原因:

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

    改正:

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

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

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

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

    不要group by er.uid,er.exam_id

    正确答案:

    1. #2、再筛选出试卷——difficulty = hard
    2. select uid,
    3. round(avg((if(score is NULL,0,score))),0) avg_score,
    4. round(avg(if(submit_time is NULL,ei.duration,timestampdiff(minute,start_time,submit_time))),1) avg_time_took
    5. from exam_record er join examination_info ei
    6. on er.exam_id = ei.exam_id
    7. where uid in #1、先筛选出用户——level=0
    8. (select uid from user_info
    9. where level = 0)
    10. and ei.difficulty = 'hard'
    11. group by er.uid
    12. # 未完成的默认试卷最大考试时长
    13. # if(submit_time is NULL,ei.duration,timestampdiff(minute,start_time,submit_time))
    14. # 未完成的默认0分处理
    15. # if(score is NULL,0,score)
  • 相关阅读:
    Spring——依赖注入
    从0开始的计组学习!让我们踏上计组的奇妙学习之旅叭~
    异步编程:CompletableFuture详解使用
    字符串常用方法 --- 字符串对象的属性 与 字符串对象的方法(上)
    学生信息(c++基础)
    深度强化学习入门:马尔可夫决策过程(井字棋案例理解)
    [JDK8下的HashMap类应用及源码分析] 数据结构、哈希碰撞、链表变红黑树
    Linux学习系列:在CentOS 7上切换WiFi节点
    生命在于折腾——某国外cms代码审计
    使用html2canvas将html元素保存为图片
  • 原文地址:https://blog.csdn.net/qq_47966193/article/details/139598113