SQL—group by和count函数、where筛选

题目:求每个学校(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

 

posted @ 2023-03-16 23:43  新手小白之倔强智商  阅读(161)  评论(0)    收藏  举报