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

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

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 外面再加一层查询都是可以的。

这题的有点刁钻了。

全部评论

相关推荐

owwhy:难,技术栈在嵌入式这块显得非常浅,并且简历有大问题。教育经历浓缩成两行就行了,写什么主修课程,说的不好听这块没人在意,自我评价删了,项目写详细点,最终简历缩成一页。相关技能怎么说呢,有点差了,还写成这么多行
投了多少份简历才上岸
点赞 评论 收藏
分享
01-11 08:47
门头沟学院 Java
choumoduji...:读研的目的就是为了以最快的速度和最低的要求完成“学校”规定的毕业标准,而不是所谓课题组的要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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