题解 | 每个学校的平均年龄和平均绩点及整体情况
每个学校的平均年龄和平均绩点及整体情况
https://www.nowcoder.com/practice/d686ac1d09d94441be91475843797d2d
select university,avg_age,avg_gpa from (select ifnull(university,"总体") university,round(avg(age),3) avg_age,round(avg(gpa),3) avg_gpa from user_profile group by university with rollup) AS a order by (case when university="总体" then 1 else 2 end)
wih rollup 用在group by 后面,对分组后的数值进行求和。
由于不能求和的字段会变为 null 需要用ifnull函数进行填充
为了保证总和可以排在第一排,用case when以后做升序排序