题解 | #计算男生人数以及平均GPA#

https://www.nowcoder.com/practice/7d9a7b2d6b4241dbb5e5066d7549ca01

select 
    # 这里用distinct 排除重复device_id情况
    count(distinct device_id) as male_num,
    avg(gpa) as avg_gpa
from user_profile
group by gender
having gender='male'
这里说下两种解法吧
  1. select
      count(gender) as male_num,
      round(avg(gpa), 1) as avg_gpa
    from user_profile where gender="male";
    一般是建议直接用group by 然后 having, 这样的效率会略高
全部评论

相关推荐

2 3 评论
分享
牛客网
牛客企业服务