题解 | #未完成试卷数大于1的有效用户#

未完成试卷数大于1的有效用户

http://www.nowcoder.com/practice/46cb7a33f7204f3ba7f6536d2fc04286

明确题意:

统计2021年每个未完成试卷作答数大于1的有效用户的数据(有效用户指完成试卷作答数至少为1且未完成数小于5),输出用户ID、未完成试卷作答数、完成试卷作答数、作答过的试卷tag集合,按未完成试卷数量由多到少排序

问题拆解:

  • 本题主要是考察知识点:case when、group by、date_format、group_concat
  • DATE_FORMAT(submit_time, "%Y%m" ) 返回的是202109这样;
  • 语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc  ] [separator '分隔符'] )。说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。
  • t3对应的是 未完成试卷作答数大于1的有效用户的数据(有效用户指完成试卷作答数至少为1且未完成数小于5)
  • t3需要与t4关联,得到uid的作答明细
  • t3需要与t5关联,得到tag
  • 最后group by,用group_concat计算

代码实现:

select 
t3.uid,
t3.incomplete_cnt,
t3.complete_cnt,
group_concat(distinct concat(date_format(t4.start_time,'%Y-%m-%d'),':',t5.tag) order by t4.start_time separator ';') as detail
from
(
    select * from 
    (
    select * 
        from (
            select 
            uid ,
            count(case when submit_time is not null then 1 else null end) as complete_cnt ,
            count(case when submit_time is null then 1 else null end) as incomplete_cnt 
            from exam_record where year(start_time) = '2021'
            group by uid
        )t1 where complete_cnt >= 1 and  incomplete_cnt < 5 
    )t2 where incomplete_cnt >1 -- complete_cnt >= 1 and  incomplete_cnt >=2 and incomplete_cnt<=4
)t3
 join exam_record t4
on t3.uid = t4.uid
 join examination_info t5
on t4.exam_id = t5.exam_id
where year(t4.start_time) = '2021'  -- 这里需要再次卡这个条件,因为join exam_record后可能会有2020年的记录。
group by t3.uid   
order by incomplete_cnt desc ;
end

全部评论

相关推荐

评论
4
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
12397次浏览 109人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7801次浏览 43人参与
# 米连集团26产品管培生项目 #
6490次浏览 219人参与
# 你的实习产出是真实的还是包装的? #
2179次浏览 44人参与
# 简历第一个项目做什么 #
31866次浏览 346人参与
# 长得好看会提高面试通过率吗? #
1222次浏览 24人参与
# MiniMax求职进展汇总 #
24378次浏览 312人参与
# 重来一次,我还会选择这个专业吗 #
433693次浏览 3926人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187394次浏览 1122人参与
# 牛客AI文生图 #
21472次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152634次浏览 888人参与
# 研究所笔面经互助 #
119006次浏览 577人参与
# 简历中的项目经历要怎么写? #
310618次浏览 4232人参与
# AI时代,哪些岗位最容易被淘汰 #
64158次浏览 840人参与
# 面试紧张时你会有什么表现? #
30537次浏览 188人参与
# 你今年的平均薪资是多少? #
213332次浏览 1039人参与
# 你怎么看待AI面试 #
180387次浏览 1273人参与
# 高学历就一定能找到好工作吗? #
64353次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76730次浏览 374人参与
# 我的求职精神状态 #
448256次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363846次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160741次浏览 1114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务