题解 | #分组计算练习题#

分组计算练习题

https://www.nowcoder.com/practice/009d8067d2df47fea429afe2e7b9de45

select gender, university, count(*), avg(active_days_within_30), avg(question_cnt)
from user_profile
group by  gender, university

group by university,gender 反过来也可以

但是如下情况不行,会报错:不满足函数依赖

select gender, university, count(*), avg(active_days_within_30), avg(question_cnt)
from user_profile
group by gender

select gender, university, count(*), avg(active_days_within_30), avg(question_cnt)
from user_profile
group by university

报错分别是:

SQL_ERROR_INFO: "Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'user_profile.university' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

SQL_ERROR_INFO: "Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'user_profile.gender' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

究其原因,比如只group by gender

那么university是有很多个的,不能select显示出来,但是语句中select gender, university,所以报错

select gender, ...., group by gender就不会报错了,虽然不是正确答案,如下所示

select gender, count(*), avg(active_days_within_30), avg(question_cnt)
from user_profile
group by   gender

全部评论

相关推荐

01-05 09:14
同济大学 Java
不要盒我呀:我要是9✌🏻我就选保研,保研了大四再找实习,实习之后,如果觉得自己不适合互联网工作模式,还能有其他选择,如果实习后决定了走互联网,也能提升学历提高竞争力
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务