sql语句查询某一个学生的大学每年的平均绩点(实例教程)
- 作者: 美女你很内涵
- 来源: 51数据库
- 2021-07-09
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.`学生姓名`;
- SQLite数据库操作:原生操作,GreenDao操作讲解
- 数据库SQL实战题:获取员工其当前的薪水比其manager当前薪水还高的相关信息(教程)
- SQLSERVER查询区分大小写的写法分析
- SQL学习总结之SQL的分类介绍
- 阶梯到高级T-SQL 1级:高级T-SQL介绍交叉连接
- 高级T-SQL级别1的Stairway:使用CROSS JOIN引入高级T-SQL分析
- 什么是SQL隔离级别?四个SQL隔离级别定义介绍
- Sql递归介绍之用with实现递归查询
- SQLSERVER查询时日期格式化的实例讲解
- 数据库SQL实战:从titles表获取按照title进行分组,注意对于重复的emp_no进行忽略(题解)