SQL—对学校和性别进行分组,计算用户活跃度和发帖数量()

题目:求每个学校(university)每种性别(gender)的用户数、30天内平均平均活跃天数(active_days_within_30)和平均发帖数量(question_cnt)。

我的尝试:

select count(device_id) as user_num,

round(AVG(active_days_within_30),1) as avg_active_day,

round(AVG(question_cnt),1) as avg_question_cnt,

from user_profile

where group by university and gender

错误点(暂时发现的):

1、语法错误where和group by连用是不对的,删掉where

2、越界数组行为(虽然不知道牛客咋判定的越界,但是我没觉得我越界了(气壮!)):看了讨论区,在select后面加了个university和gender,又出现了越界行为(喵喵喵的!)要删掉and。

敲!还要注意顺序,答案:

select

gender,

university,

count(device_id) as user_num,

round(AVG(active_days_within_30),1) as avg_active_day,

round(AVG(question_cnt),1) as avg_question_cnt,

from user_profile

group by gender,university

————————

题目:求每个学校(university)每种性别(gender)的用户数、30天内平均平均活跃天数(active_days_within_30)和平均发帖数量(question_cnt)。

我的尝试:

select count(device_id) as user_num,

round(AVG(active_days_within_30),1) as avg_active_day,

round(AVG(question_cnt),1) as avg_question_cnt,

from user_profile

where group by university and gender

错误点(暂时发现的):

1、语法错误where和group by连用是不对的,删掉where

2、越界数组行为(虽然不知道牛客咋判定的越界,但是我没觉得我越界了(气壮!)):看了讨论区,在select后面加了个university和gender,又出现了越界行为(喵喵喵的!)要删掉and。

敲!还要注意顺序,答案:

select

gender,

university,

count(device_id) as user_num,

round(AVG(active_days_within_30),1) as avg_active_day,

round(AVG(question_cnt),1) as avg_question_cnt,

from user_profile

group by gender,university