【发布时间】:2019-05-25 10:10:03
【问题描述】:
查询应输出学生的姓名和 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);
课程:
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 ;
@Barbaros Özhan 的建议以及我自己的类似解决方案返回的结果是:
'1', '史蒂夫因斯基普', '1.7857'
作为返回的第一行。
但通过观察注册表,学生 #1 的 GPA 显然不是 1.7857。
编辑:戈登·林诺夫回答:
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 ;
【问题讨论】:
-
成绩是否必须按课程单元加权?
-
@Nick 未指定。
-
@RichardMichaelLaboe 。 . .你说的废话输出是什么意思?查询看起来没问题。
-
@GordonLinoff 我刚刚在我的编辑中解释了它。
-
请参阅Why should I provide an MCVE for what seems to me to be a very simple SQL query? 以提供示例数据和预期结果。此外,您还必须解释 GPA 是什么,我假设它是平均绩点?...