题目:查看每个学校的平均发帖数(avg_question_cnt)和平均回帖数(avg_answer_cnt),并取出平均发帖数小于5的学校和平均回帖数小于20的学校。(保留3位小数)
大佬的分解:
1、限定条件:平均发帖数小于5和平均回帖数小于20的学校:avg_question_cnt<5 and avg_answer_cnt<20
##重点:聚合函数结果作为筛选条件时,不能用where,选择用having
2、按学校输出:对每个学校计算平均回帖数和平均发帖数group by university
细节问题:
1、重命名用as
2、用having,不用where
代码示例:
select
university,
round(avg(question_cnt),3) as avg_question_cnt,
round(avg(answer_cnt),3) as avg_answer_cnt
from user_profile
group by university
having avg_question_cnt<5 and avg_answer_cnt<20
标签:cnt,group,函数,question,SQL,avg,发帖数,having From: https://www.cnblogs.com/buki233/p/17242062.html