当前位置: 移动技术网 > IT编程>数据库>MSSQL > sql语句查询某一个学生的大学每年的平均绩点(实例教程)

sql语句查询某一个学生的大学每年的平均绩点(实例教程)

2019年01月05日  | 移动技术网IT编程  | 我要评论

天天向上101015,阿凡达 电影天堂,杨愿成

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.`学生姓名`;

\

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网