题解 | #每个学校的平均年龄和平均绩点及整体情况#
每个学校的平均年龄和平均绩点及整体情况
https://www.nowcoder.com/practice/d686ac1d09d94441be91475843797d2d
可用两种方法实现:
# # 方法一:使用rollup汇总 # SELECT ifnull(university,'总体')university, # round(avg(age),3)avg_age, # round(avg(gpa),3)avg_gpa # FROM user_profile # GROUP BY university # WITH rollup # ORDER BY university # ; # 方法二:使用union SELECT '总体' university, round(avg(age),3)avg_age, round(avg(gpa),3)avg_gpa FROM user_profile UNION SELECT university, round(avg(age),3)avg_age, round(avg(gpa),3)avg_gpa FROM user_profile GROUP BY university ;