2013-10-03 255 views
-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 
+1

將來,如果您還發布表格定義和您看到的錯誤信息,它確實會有所幫助。 –

回答

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

ClassCourses是內部查詢表的別名。 –

+0

yep剛剛注意到 –

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 
+0

+1。 –

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; 
+0

錯過最終的GROUP BY。 –

+0

添加了group by子句。 – Teja

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 
3

即使在外部查詢別名&集團通過更正,僅供參考最大值(T.SumOfMarks)和Min(T.SumOfMarks)會產生相同的值。

+0

你是對的。也許一個如何解決這個問題的例子可能會讓你的答案被接受! –

+0

Duh,當然。不能相信我們都錯過了:) –