SQL语句 求各系、各班级的人数和平均年龄,表为Student,分组应该怎么分

2024-11-17 13:41:54
推荐回答(2个)
回答1:

select 系名,count(*) as 系人数, avg(年龄) as 系平均年龄
from Student group by 系名
select 班级名,count(*) as 班级人数, avg(年龄) as 班级平均年龄
from Student group by 班级名
如果系名和班级名字段类型一样,可以用union all 把两个表连起来。如果不一样,可以把班级名转换成和系名字段类型一样,再用union all连接起来。

回答2:

系和班需要分两个不同语句来写
select className,count(*) as number, avg(age) as averageAge
from Student group by className

select departmentName,count(*) as number, avg(age) as averageAge
from Student group by departmentName