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

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

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

select tag,count(tag) as tag_cnt
from
(
select tag from
(select exam_id from
    (
        select * from exam_record er
        where er.uid in
        (
            # 查找月均完成试卷数不小于3的用户list
            select uid from
            (
            select 
                uid, 
                round(count(date_format(submit_time, '%Y%m')) / count(distinct date_format(submit_time, "%Y%m")), 2) as complete_avg
            from exam_record
            group by uid
            having complete_avg >= 3
            ) as over_three_1
        )
    ) as over_three_2
) as eti
left join examination_info ei
on eti.exam_id = ei.exam_id
) as tct
group by tag
order by tag_cnt desc;

【知识点】子查询的别名问题、date_format函数、GROUP BY、HAVING

date_format(submit_time, '%Y%m')——后面的格式需要加引号,不然会报错

SELECT xxx FROM (子查询) AS [别名]——子查询得到的表要加别名,不然会报错

WHERE xxx IN (子查询)——这里的我没加,通过了

查了一下子查询的别名问题,这个例题正好把两种情况都涉及到了:

【参考链接】

https://zhuanlan.zhihu.com/p/336820866

全部评论

相关推荐

昨天 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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