题解 | #月均完成试卷数不小于3的用户爱作答的类别#要求的是每月>=3的用户

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

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

一开始没多想,看了看题目,求答题人数>=3。连表,求职,倒序结束。

注意点:要求的是每月>=3的用户,而不是全部答题>=3

第一次报错:



select tag,count(tag) tag_cnt from exam_record e1
 join
(select uid,sum(if(submit_time is not null,1,0)) cnt from exam_record
group by uid having cnt >=3) a
on e1.uid=a.uid
join examination_info e2
on e1.exam_id=e2.exam_id
group by tag
order by tag_cnt desc

因为题目还有一句,当月>=3.上边的语句没考虑这个条件。把语句的第一段子查询放到筛选条件中

通过:

select
tag,count(tag) tag_cnt
from exam_record e1
join examination_info e2
on e1.exam_id=e2.exam_id
where uid in
(select distinct uid from  (select uid,date_format(start_time,"%Y-%m"),sum(if(submit_time is not null,1,0)) cnt from exam_record
group by uid,date_format(start_time,"%Y-%m") having cnt >=3)  a)
group by tag
order by tag_cnt desc
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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