-1

任何人都可以更正这个查询吗?我只是不知道如何使它工作。

 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
4

5 回答 5

3

即使在外部查询中更正了别名和分组依据后,仅供参考 Max(T.SumOfMarks) 和 Min(T.SumOfMarks) 将产生相同的值。

于 2013-10-03T15:04:59.757 回答
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 行。

于 2013-10-03T14:52:01.890 回答
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
于 2013-10-03T14:50:48.823 回答
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;
于 2013-10-03T14:51:50.670 回答
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
于 2013-10-03T14:57:10.740 回答