配置单元中不同列上的sql平均函数

6g8kf2rb  于 2021-06-28  发布在  Hive
关注(0)|答案(2)|浏览(231)

我想用配置单元查询找到3列的平均值。考虑以下数据:

我需要找到每个学生的平均得分,然后是每个学校总得分的平均值:null应该被忽略。
我的输出应该如下所示:

你们能帮帮我吗

bbmckpt7

bbmckpt71#

配置单元应自动忽略 NULL 此处报告的骨料值。
为了可读性,我建议使用 COALESCE 而不是 IF IS NULL 陈述如下: COALESCE(Math,0) as Math

vmdwslir

vmdwslir2#

每个学生的平均成绩:

select school,SL_No,Name,Math,Phy,Chem,(if(Math is NULL,0,Math)+if(Phy is NULL,0,Phy)+if(Chem is NULL,0,Chem))/3 as avg_marks from my_table

各校平均分

select school,avg(avg_marks) from (select school,SL_No,Name,Math,Phy,Chem,(if(Math is NULL,0,Math)+if(Phy is NULL,0,Phy)+if(Chem is NULL,0,Chem))/3 as avg_marks from my_table
) temp group by school

相关问题