思路:使用GROUP BY
关键字对数据进行分组,即不含有重复的数据,用来统计数据
代码解释:对数据进行分组,注意这里性别和学校都有可能重复,所以都要进行分组。然后从表user_profile
中取出列gender
、university
以及一些计算数据
SELECT gender, university, COUNT(gender) AS user_num, AVG(active_days_within_30) AS avg_active_day, AVG(question_cnt) AS avg_question_cnt FROM user_profile GROUP BY gender, university
题目:https://www.nowcoder.com/practice/009d8067d2df47fea429afe2e7b9de45?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D199