sql语句查询某一个学生的大学每年的平均绩点(实例教程)
1.要求:以一个同学为例,计算出某一年度的平均绩点,以sql语句的形式输出。
2.绩点求法:相应科目绩点乘相应科目学分,求和,
(sc_gpa * sc_course_credit)/(sum(sc_course_credit))
然后除所有学分的和,就是你的平均绩点。
学分越高的科目对平均绩点影响越大
3.步骤:
(1)先查询该学生这一年的成绩
select sc_student_name as 学生姓名,sc_course_name as 课程名称,
sc_course_credit as 课程学分,sc_gpa as 课程绩点
from t_score
where sc_student_id = "1120108116"
and sc_edu_year = "2015-2016";
(2)然后根据绩点算法算出该学生这一年的平均绩点
查询实例
select sc.`学生姓名`,
sum(sc.课程学分 * ifnull(sc.课程绩点,0))/sum(sc.`课程学分`) as 平均绩点
from (select sc_student_name as 学生姓名,sc_course_name as 课程名称,
sc_course_credit as 课程学分,sc_gpa as 课程绩点
from t_score
where sc_student_id = "1120108116"
and sc_edu_year = "2015-2016" ) as sc
where 1=1;
group by sc.`学生姓名`;
继续考虑小数点的取舍,修改查询语句
select sc.`学生姓名`,
round(sum(sc.课程学分 * ifnull(sc.课程绩点,0))/sum(sc.`课程学分`),1) as 平均绩点
from (select sc_student_name as 学生姓名,sc_course_name as 课程名称,
sc_course_credit as 课程学分,sc_gpa as 课程绩点
from t_score
where sc_student_id = "1120108116"
and sc_edu_year = "2015-2016" ) as sc
where 1=1;
group by sc.`学生姓名`;
上一篇: 管窥HTML5