题解 | 分组过滤练习题

分组过滤练习题

https://www.nowcoder.com/practice/ddbcedcd9600403296038ee44a172f2d

题意分析:

请取出平均发贴数低于5的学校或平均回帖数小于20的学校。

  • 使用函数round(), avg()。前者使用在确定小数的后几位,后者使用与计算某一字段的平均数,即这个字段的下的所有数值加起来在除以字段显示的条数。就是这个字段的平均值。
  • 最后通过使用group by university 分组学校

最后我使用嵌合查询

# 保留三位小数 round函数
select T.university, T.avg_question_cnt, T.avg_answer_cnt
from
(
    select university, round(avg(question_cnt), 3) avg_question_cnt, 
    round(avg(answer_cnt), 3) avg_answer_cnt
    from user_profile
    group by university
) T
where T.avg_question_cnt < 5 or T.avg_answer_cnt < 20

在题解区看大佬使用的是having 函数,6666

having 函数,对新写的列名avg_question_cnt 和 avg_answer_cnt 进行查询

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 和 avg_answer_cnt 进行查询
HAVING
avg_question_cnt<5  or
avg_answer_cnt<20

#牛客创作赏金赛#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务