题解 124 | #统计作答次数#

【场景】:不同条件下做统计

【分类】:select if、多表连接

分析思路

难点:

1.在不同的条件下使用count做统计

在不同的条件下做统计

方法一:

where 中写条件,每次只能得到一列,分别得到每列,然后用join做连接

方法二:

使用select if,把条件写在select里面

作答次数

  • [条件]: count(idid)

已完成的作答次数

  • [条件]: count(if(submit_time is not null and score is not null, 1, null))

已完成的试卷份数

  • [条件]:count(distinct if(submit_time is not null and score is not null, exam_id, null))

求解代码

方法一:

with子句 + where

with
    temp as(
        #作答次数
        select
            count(id) as total_pv
        from exam_record
    )
    ,main as(
        #已完成的作答次数
        select
            count(if(submit_time is not null and score is not null, 1, null)) as complete_pv
        from exam_record
    )
    ,attr as(
        #已完成的试卷份数
        select
            count(distinct if(submit_time is not null and score is not null, exam_id, null)) as complete_exam_cnt
        from exam_record
    )

#多表连接
select *
from temp,main,attr

方法二:

多表连接 + where

#多表连接
select *
from(
    #作答次数
    select
        count(id) as total_pv
    from exam_record
) temp,(
    #已完成的作答次数
    select
        count(if(submit_time is not null and score is not null, 1, null)) as complete_pv
    from exam_record
)main,(
    #已完成的试卷份数
    select
        count(distinct if(submit_time is not null and score is not null, exam_id, null)) as complete_exam_cnt
    from exam_record
)attr

方法三:

使用select if

select
    count(id) as total_pv,
    count(if(submit_time is not null and score is not null, 1, null)) as complete_pv,
    count(distinct if(submit_time is not null and score is not null, exam_id, null)) as complete_exam_cnt
from exam_record
全部评论

相关推荐

点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
评论
13
17
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务