题解 | #每个题目和每份试卷被作答的人数和次数#

每个题目和每份试卷被作答的人数和次数

https://www.nowcoder.com/practice/203d0aed8928429a8978185d9a03babc

select t.tid ,
count(distinct uid) as uv,
count(*)
from (
select uid,exam_id as tid from exam_record
union 
select uid,question_id from practice_record)t
group by t.tid 

由于这道题要求对 试卷和练习的结果分别排序,所以上方的写法就没有办法实现了。

with t1 as 

(select  exam_id as tid , count(distinct uid) as uv ,
count(*) as pv
from exam_record
group by tid order by uv desc,pv desc),
t2 as 
(select  question_id as tid , count(distinct uid) as uv ,
count(*) as pv
from practice_record
group by tid order by uv desc ,pv desc)

select * from t1 
union 
select * from t2

由于union 的排序最能跟在最后的语句中,即只出现一次。

所以用临时表的方式,或者在每个union 外面再加一层查询都是可以的。

这题的有点刁钻了。

全部评论

相关推荐

点赞 评论 收藏
分享
02-10 12:23
已编辑
新余学院 C++
采集想要offer:专业技能那里要一条一条的列出来吧,感觉你项目很厉害了,但是如果你不写技术栈面试官对你项目不太懂的话都没办法问你八股😂C++都是基架岗,都是一群9✌🏻在卷,我觉得你要是有时间学个go把MySQL和redis写上去找个开发岗吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务