mysql - 如何在 MySql 中计算 GPA

标签 mysql sql

查询应该输出学生的姓名和 GPA。

给出了下表:

部分:

CREATE TABLE `Section` (
  `ID` int(11) NOT NULL,
  `Semester` varchar(45) DEFAULT NULL,
  `Room` varchar(45) DEFAULT NULL,
  `Instructor_ID` int(11) NOT NULL,
  `Course_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `fk_Section_Instructor_idx` (`Instructor_ID`),
  KEY `fk_Section_Course1_idx` (`Course_ID`),
  CONSTRAINT `fk_Section_Course1` FOREIGN KEY (`Course_ID`) REFERENCES `Course` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_Section_Instructor` FOREIGN KEY (`Instructor_ID`) REFERENCES `Instructor` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `Section` VALUES (1,'Fa17','828',1,1),(2,'Fa17','828',2,3),(3,'Fa17','829',1,4),(4,'Fa17','829',4,5),(5,'Sp18','828',1,1),(6,'Sp18','829',1,2),(7,'Sp18','828',3,4),(8,'Sp18','828',4,5);

类(class):

DROP TABLE IF EXISTS `Course`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Course` (
  `ID` int(11) NOT NULL,
  `Title` varchar(45) DEFAULT NULL,
  `Description` text,
  `Units` int(11) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `Course` VALUES (1,'CIS-15','Cloud Programming in Python',4),(2,'CIS-54','Relational Databases',4),(3,'CIS-81','Introduction to Networking',4),(4,'CIS-75','Introduction to Computer Security',3),(5,'CIS-90','Introduction to Linux',3);

注册:

 CREATE TABLE `Registration` (
  `Section_ID` int(11) NOT NULL,
  `Student_ID` int(11) NOT NULL,
  `Grade` int(11) DEFAULT NULL,
  PRIMARY KEY (`Section_ID`,`Student_ID`),
  KEY `fk_Section_has_Student_Student1_idx` (`Student_ID`),
  KEY `fk_Section_has_Student_Section1_idx` (`Section_ID`),
  CONSTRAINT `fk_Section_has_Student_Section1` FOREIGN KEY (`Section_ID`) REFERENCES `Section` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_Section_has_Student_Student1` FOREIGN KEY (`Student_ID`) REFERENCES `Student` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `Registration` VALUES (1,1,4),(1,2,4),(2,2,3),(3,3,2),(4,1,3),(4,3,3),(5,3,NULL),(5,4,NULL),(6,1,NULL),(6,2,NULL),(7,1,NULL),(7,4,NULL),(8,2,NULL),(8,3,NULL);

学生:

CREATE TABLE `Student` (
  `ID` int(11) NOT NULL,
  `Name` varchar(45) DEFAULT NULL,
  `Email` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `Student` VALUES (1,'Steve Inskeep','steve@xyz.edu'),(2,'Rene Montaign','rene@xyz.edu'),(3,'David Green','david@xyz.edu'),(4,'Rachel Martin','rachel@xyz.edu');

我试过这段代码试过这个并得到了无意义的输出。我迷路了

SELECT student.Name, (sum( registration.grade * course.units) /
    sum(course.units)
   ) as GPA FROM registration 

   join student on registration.student_ID = student.id join section on  section.ID = registration.section_ID
   join course on section.course_ID = course.ID
   group by registration.student_ID  ;

GPA 似乎是错误的,因为 enter image description here

@Barbaros Özhan 的建议以及我自己的类似解决方案的返回结果是:

“1”、“史蒂夫因斯基普”、“1.7857”

作为第一个返回的行。

但从登记表中可以清楚地看出学生 #1 的 GPA 不是 1.7857。

编辑:Gordon Linoff 回答:

 select student.Name,
       (sum( registration.grade * course.units) /
        sum( case when registration.grade is not null then course.units end )
       ) as GPA
from registration  join 
     student 
     on registration.student_ID = student.id join
     section 
     on section.ID = registration.section_ID join
     course 
     on section.course_ID = course.ID
group by student.ID  ;

最佳答案

问题是您的成绩为 NULL。 . .但您计算的是部分,因此这些部分被视为零。

稍微调整一下您的计算即可解决此问题:

select s.Name,
       (sum( r.grade * c.units) /
        sum( case when r.grade is not null then c.units end )
       ) as GPA
from registration r join 
     student s
     on r.student_ID = s.id join
     section se
     on se.ID = r.section_ID join
     course c
     on se.course_ID = c.ID
group by s.student_ID  ;

关于mysql - 如何在 MySql 中计算 GPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299938/

相关文章:

mysql - Web 应用程序无法从服务器上的 MySQL 查询

php - SQL 查询在表中显示时丢失数据

php - 如何使用 PHP 跟踪数据库表中的更新?

php - 将 CSV 内容保存到 mysql 数据库中

sql - 如何从 Oracle 10G PL/SQL 函数和过程中查找所有表引用?

sql - 从LEFT OUTER JOIN删除重复项

PHP、MySQL CSV 导入 - 您会如何执行此操作?

SQL查询: find all devices that have ALL of the Ids passed in

Mysql在多边形中找到相交的lat lng点

PHP/SQL - 如何过滤掉与另一个表的数据冲突的记录?