题解 | #月均完成试卷数不小于3的用户爱作答的类别#

月均完成试卷数不小于3的用户爱作答的类别

https://www.nowcoder.com/practice/b1d13efcfa0c4ecea517afbdb9090845

/*
1、表拼接,exam_record left join examination_info using(exam_id)
2、group by uid,计算月均完成数,having 大于等于3
3、1和2作为一个子表,返回符合条件的uid
4、用IN在exam_record中统计

*/
/*
with tmp as(
    select uid, exam_id, tag, start_time, submit_time, score
    from exam_record left join examination_info using(exam_id)
)

select
    tag,
    count(tag) as tag_cnt
from tmp
where uid in 
    (select uid
    from tmp
    group by uid, month(start_time)
    having count(submit_time)>=3)
group by tag
order by tag_cnt desc
*/

/*优化*/
/*先查“当月均完成试卷数”不小于3的用户id,将结果作为子表,再需要联表进行查询*/
select tag, count(tag) as tag_cnt
from exam_record left join examination_info using(exam_id)
where uid in 
    (select uid from exam_record group by uid, month(start_time) having count(submit_time)>=3)
group by tag
order by tag_cnt desc

全部评论

相关推荐

04-18 15:58
已编辑
门头沟学院 设计
kaoyu:这一看就不是计算机的,怎么还有个排斥洗碗?
点赞 评论 收藏
分享
白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务