查询应输出学生的姓名和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','Steve Inskeep','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 ;
答案 0 :(得分:1)
考虑到学生每节的成绩,我认为您在查询中不需要Section
和Course
表(尽管没有看到示例数据,这很难确定)。这应该起作用:
SELECT s.Name, COALESCE(SUM(r.Grade) / COUNT(r.Grade), 0) AS GPA
FROM student s
JOIN registration r ON r.Student_ID = s.ID
GROUP BY s.Name
输出
Name GPA
David Green 2.5
Rachel Martin 0
Rene Montaign 3.5
Steve Inskeep 3.5
如果需要根据课程单位对成绩进行加权,则需要将成绩乘以单位求和,然后除以单位总和,请注意仅求和有效分数的课程单位:
SELECT s.Name,
ROUND(COALESCE(SUM(r.Grade * c.Units) / SUM(CASE WHEN r.Grade IS NOT NULL THEN c.Units ELSE 0 END), 0), 2) AS GPA
FROM Student s
JOIN Registration r ON r.Student_ID = s.ID
JOIN Section x ON x.ID = r.Section_ID
JOIN Course c ON c.ID = x.Course_ID
GROUP BY s.Name;
输出:
Name GPA
David Green 2.5
Rachel Martin 0
Rene Montaign 3.5
Steve Inskeep 3.57
答案 1 :(得分:1)
您可以按以下方式加入表格:
select s.ID, s.Name,
sum( r.grade * c.Units ) / sum(c.Units) as GPA
from student s
left join registration r on r.Student_ID = s.ID
left join section sc on sc.ID = r.Section_ID
left join course c on c.ID = sc.Course_ID
group by s.ID, s.Name
答案 2 :(得分:1)
问题是您拥有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 ;
答案 3 :(得分:0)
您正在从注册表中加入学生ID,但尚未从注册表中选择该学生ID即可加入。尝试将学生ID添加到注册表中的选择中。
然后您将课程表连接到分区表,但是分区表尚未连接。操作无效,您需要先加入本节,然后才能从本节加入课程。
可能还有其他麻烦,但从这里开始。