题解 | #满足条件的用户的试卷完成数和题目练习数#

满足条件的用户的试卷完成数和题目练习数

http://www.nowcoder.com/practice/5c03f761b36046649ee71f05e1ceecbf

select a.uid, 
	count(distinct 
      	case when year(b.submit_time) = '2021' then b.id 
      	else null end) as exam_cnt, 
	count(distinct 
     	case when year(c.submit_time) = '2021' then c.id
     	else null end) as question_cnt
from (select a.uid
  	from user_info as a 
  	inner join exam_record as b using(uid)
  	inner join examination_info as ei using(exam_id)
  	where a.level = 7 and ei.tag = 'SQL' and ei.difficulty = 'hard' 
  	and b.submit_time is not null
  	group by a.uid 
  	having avg(b.score) >80
 	) as a 
left join exam_record as b on a.uid = b.uid
left join practice_record as c on a.uid = c.uid
group by a.uid
order by exam_cnt, question_cnt desc

第三行count用于b.id是因为这几个表的主键都是ID,用sum if函数应该也可以 大概思路就是先把高难度、SQL、7级、均分大于80 这几个满足条件的用户id筛选出来,注意第16行临时表一定要有别名。然后再增加其他筛选条件。

全部评论

相关推荐

05-21 15:47
门头沟学院 Java
浪漫主义的虹夏:项目有亮点吗,第一个不是纯玩具项目吗,项目亮点里类似ThreadLocal,Redis储存说难听点是花几十分钟绝大部分人都能学会,第二个轮子项目也没体现出设计和技术,想实习先沉淀,好高骛远的自嗨只会害了自己
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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