题解 | #月均完成试卷数不小于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

全部评论

相关推荐

11-28 17:58
门头沟学院 Java
美团 JAVA开发 n×15.5
牛客786276759号:百度现在晋升很难的 而且云这块的业务没美团好 你看百度股价都跌成啥样了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务