有人可以更正此查询吗? 我只是不知道如何让它发挥作用。
SELECT ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark,Max(SumOFMarks),Min(SumOFMarks)
FROM
(
SELECT ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark,sum(StudentMark)SumOFMarks
FROM Users
INNER JOIN UserExams ON Users.UserID = UserExams.UserID
INNER JOIN Exams ON UserExams.ExamID = Exams.ExamID
INNER JOIN ClassCourses ON Exams.ClassID = ClassCourses.ClassID AND Exams.CourseID = ClassCourses.CourseID
WHERE ClassCourses.ClassID=1
group by ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark
)AS T
答案 0 :(得分:3)
试试这个:
SELECT T.ClassID,
T.CourseID,
T.MaxMark,
Max(SumOFMarks),
Min(SumOFMarks)
FROM
(
SELECT ClassCourses.ClassID,
ClassCourses.CourseID,
ClassCourses.MaxMark,
sum(StudentMark) SumOFMarks
FROM Users
INNER JOIN UserExams ON Users.UserID = UserExams.UserID
INNER JOIN Exams ON UserExams.ExamID = Exams.ExamID
INNER JOIN ClassCourses ON Exams.ClassID = ClassCourses.ClassID AND Exams.CourseID = ClassCourses.CourseID
WHERE ClassCourses.ClassID=1
group by ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark
)AS T
GROUP BY T.ClassID,
T.CourseID,
T.MaxMark
外部SELECT语句缺少GROUP BY
子句 - 我已添加它。看看最后3行。
答案 1 :(得分:3)
甚至在更正别名之后& Group By外部查询,FYI Max(T.SumOfMarks)和Min(T.SumOfMarks)将产生相同的值。
答案 2 :(得分:1)
最后只需GROUP BY
并修复表别名:
SELECT T.ClassID,
T.CourseID,
T.MaxMark,
MAX(T.SumOFMarks),
MIN(T.SumOFMarks)
FROM
(
SELECT ClassCourses.ClassID,
ClassCourses.CourseID,
ClassCourses.MaxMark,
SUM(StudentMark) SumOFMarks
FROM Users
INNER JOIN UserExams ON Users.UserID = UserExams.UserID
INNER JOIN Exams ON UserExams.ExamID = Exams.ExamID
INNER JOIN ClassCourses ON Exams.ClassID = ClassCourses.ClassID AND Exams.CourseID = ClassCourses.CourseID
WHERE ClassCourses.ClassID = 1
GROUP BY ClassCourses.ClassID,
ClassCourses.CourseID,
ClassCourses.MaxMark
)AS T
GROUP BY ClassID,CourseID,MaxMark
答案 3 :(得分:0)
您的别名在外部查询中是错误的。我删除了ClassCourses并添加了T.
SELECT T.ClassID,T.CourseID,T.MaxMark,Max(T.SumOFMarks),Min(T.SumOFMarks)
FROM
(
SELECT
ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark,sum(StudentMark)SumOFMarks
FROM Users
INNER JOIN
UserExams
ON Users.UserID = UserExams.UserID
INNER JOIN Exams
ON UserExams.ExamID = Exams.ExamID
INNER JOIN ClassCourses
ON Exams.ClassID = ClassCourses.ClassID
AND Exams.CourseID = ClassCourses.CourseID
WHERE ClassCourses.ClassID=1
group by ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark
)AS T
GROUP BY T.ClassID,T.CourseID,T.MaxMark;
答案 4 :(得分:0)
您可以像这样使用CTE:
WITH CteUsers (ClassID, CourseID, MaxMark, SumOFMarks)
AS (
SELECT ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark,sum(StudentMark)SumOFMarks
FROM Users
INNER JOIN UserExams ON Users.UserID = UserExams.UserID
INNER JOIN Exams ON UserExams.ExamID = Exams.ExamID
INNER JOIN ClassCourses ON Exams.ClassID = ClassCourses.ClassID AND Exams.CourseID = ClassCourses.CourseID
WHERE ClassCourses.ClassID=1
group by ClassCourses.ClassID,ClassCourses.CourseID,ClassCourses.MaxMark
)
SELECT ClassID,CourseID,MaxMark,Max(SumOFMarks),Min(SumOFMarks)
FROM CteUsers
GROUP BY ClassID,CourseID,MaxMark