[
SELECT r.AdNo,
r.Name,
SUM(CASE WHEN p.SubjectID = 101 THEN s.score ELSE 0 END) AS ENGLISH,
SUM(CASE WHEN p.SubjectID = 102 THEN s.score ELSE 0 END) AS KISWAHILI,
SUM(CASE WHEN p.SubjectID = 103 THEN s.score ELSE 0 END) AS MATH,
SUM(CASE WHEN p.SubjectID = 104 THEN s.score ELSE 0 END) AS CRE,
SUM(CASE WHEN p.SubjectID = 105 THEN s.score ELSE 0 END) AS 'SOCIAL STUDIES',
SUM(CASE WHEN p.SubjectID THEN s.score ELSE 0 END) AS 'TOTAL',
(sum(s.score)/count(s.SubjectID) ) AS 'AVERAGE',
g.grade_letter
FROM student r
INNER JOIN marks s ON r.AdNo = s.AdNo
INNER JOIN subject p ON s.SubjectID = p.SubjectID
INNER JOIN grades g ON 77.2 BETWEEN g.lowerlimit AND g.upperlimit
GROUP BY r.Name
order by AVERAGE DESC;
[SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for category
-- ----------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`CategoryID` int(4) NOT NULL,
`CategoryName` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `CategoryID` (`CategoryID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for grades
-- ----------------------------
DROP TABLE IF EXISTS `grades`;
CREATE TABLE `grades` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`grade_letter` char(2) DEFAULT NULL,
`lowerlimit` double DEFAULT NULL,
`upperlimit` double DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `grade_letter` (`grade_letter`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for marks
-- ----------------------------
DROP TABLE IF EXISTS `marks`;
CREATE TABLE `marks` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`AdNo` varchar(10) DEFAULT NULL,
`SubjectID` int(3) DEFAULT NULL,
`score` double DEFAULT NULL,
`CategoryID` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `AdNo` (`AdNo`,`SubjectID`,`CategoryID`),
KEY `SubjectID` (`SubjectID`),
KEY `CategoryID` (`CategoryID`),
CONSTRAINT `marks_ibfk_1` FOREIGN KEY (`AdNo`) REFERENCES `student` (`AdNo`),
CONSTRAINT `marks_ibfk_2` FOREIGN KEY (`SubjectID`) REFERENCES `subject` (`SubjectID`),
CONSTRAINT `marks_ibfk_3` FOREIGN KEY (`CategoryID`) REFERENCES `category` (`CategoryID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`Name` varchar(30) DEFAULT NULL,
`AdNo` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `AdNo` (`AdNo`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for subject
-- ----------------------------
DROP TABLE IF EXISTS `subject`;
CREATE TABLE `subject` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`SubjectID` int(3) DEFAULT NULL,
`SubjectName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `SubjectID` (`SubjectID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;]
答案 0 :(得分:0)
而是使A-的上限与A的下限相同,等等。 此外,您的成绩表并不能解释平均分数小于1的学生。在这种情况下,我的查询将返回" ???"等级。
SELECT m.*, COALESCE(grades.grade_letter, '???') AS GRADE
FROM (
SELECT student.AdNo, student.Name,
SUM(IF(subject.SubjectID = 101, marks.score, 0)) AS ENGLISH,
SUM(IF(subject.SubjectID = 102, marks.score, 0)) AS KISWAHILI,
SUM(IF(subject.SubjectID = 103, marks.score, 0)) AS MATH,
SUM(IF(subject.SubjectID = 104, marks.score, 0)) AS CRE,
SUM(IF(subject.SubjectID = 105, marks.score, 0)) AS "SOCIAL STUDIES",
SUM(IF(subject.SubjectID IS NOT NULL, marks.score, 0)) AS TOTAL,
SUM(marks.score)/COUNT(*) AS AVERAGE
FROM student
INNER JOIN marks ON student.AdNo = marks.AdNo
INNER JOIN subject ON marks.SubjectID = subject.SubjectID
GROUP BY student.AdNo
) AS m
LEFT OUTER JOIN grades
ON m.AVERAGE >= grades.lowerlimit
AND m.AVERAGE < grades.upperlimit
ORDER BY m.AVERAGE DESC;
为所有科目添加另一名0分的学生,这样做:
+--------+------+---------+-----------+------+------+----------------+-------+---------+-------+
| AdNo | Name | ENGLISH | KISWAHILI | MATH | CRE | SOCIAL STUDIES | TOTAL | AVERAGE | GRADE |
+--------+------+---------+-----------+------+------+----------------+-------+---------+-------+
| TU-003 | D K | 67 | 80 | 90 | 70 | 79 | 386 | 77.2 | A- |
| TU-004 | G K | 57 | 76 | 61 | 78 | 80 | 352 | 70.4 | B+ |
| TU-001 | G C | 67 | 68 | 78 | 54 | 76 | 343 | 68.6 | B+ |
| TU-002 | F K | 60 | 34 | 74 | 50 | 67 | 285 | 57 | B- |
| TU-005 | X X | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ??? |
+--------+------+---------+-----------+------+------+----------------+-------+---------+-------+