题解 | #每个题目和每份试卷被作答的人数和次数#
每个题目和每份试卷被作答的人数和次数
http://www.nowcoder.com/practice/203d0aed8928429a8978185d9a03babc
明确题意:
统计每个题目和每份试卷被作答的人数和次数,分别在试卷区和题目区按uv & pv降序显示
问题拆解:
- 本题主要是考察知识点:group by、count(distinct )等
- uv = count(distinct uid)
- pv = count(uid)
- 注意order by后不能直接union all,会报错。要用子表,然后再union all!!
代码实现:
select * from ( select exam_id as tid, count(distinct uid) as uv, count(*) as pv from exam_record group by exam_id order by uv desc ,pv desc -- order by 后不能直接union all 会报错, )t1 -- 这里要用一个子表 union all -- 不会去重 select * from ( select question_id, -- 这里虽然不是tid,但会以第一个表的tid字段名为准显示出来,代码不会报错!! count(distinct uid) as uv, count(*) as pv from practice_record group by question_id order by uv desc ,pv desc )t2 ; -- 这里要用一个子表
不足之处,欢迎指正