题解 | #分组过滤练习题#
分组过滤练习题
http://www.nowcoder.com/practice/ddbcedcd9600403296038ee44a172f2d
使用having过滤分组查询结果
限定条件:
- 平均发贴数低于5或平均回帖数小于20的学校,avg(question_cnt)<5 or avg(answer_cnt)<20
- 聚合函数结果作为筛选条件时,不能用where,而是用having语法,配合重命名即可;
- 按学校输出:需要对每个学校统计其平均发贴数和平均回帖数,因此group by university
select university,avg(question_cnt) as avg_question_cnt,avg(answer_cnt) as avg_answer_cnt from user_profile
group by university
having avg(question_cnt)<5 or avg(answer_cnt)<20;