2013-09-16 45 views
0

我有4個表使用組由和爲了通過在同一時間

Class(ID,Name) 
Month(ID,Name) 
Student(ID,Name) 
Examination(ID,Student,Class,Month,TotalMarks,ObtainedMarks) 

student,class,month已採取如Examination表的引用的列。

我想從Examinationorder by ObtanedMarks and Group by Class

我用下面的SQL查詢

SELECT  Months.Name, Class.Name AS Classname, Students.Name AS Studentname, Examination.* 
FROM   Class INNER JOIN 
         Examination ON Class.ID = Examination.Class INNER JOIN 
         Months ON Examination.Month = Months.ID INNER JOIN 
         Students ON Examination.Student = Students.ID 
         order by Obtained desc Group by Class.Name 

選擇記錄,但查詢是不是爲我工作。

+2

** **什麼是不工作? – Kermit

+0

你在使用什麼SQL引擎? – cubetwo1729

+1

您正在使用哪個數據庫服務器以及您看到了哪些錯誤消息? – Martin

回答

1

看起來像它的南轅北轍,Group ByOrder By之前。您或者需要包含全部您要在Group By中顯示的列,或者對不在Group By子句中的字段使用聚合函數Sum,Max等。

見更新的查詢......

SELECT Months.Name , 
     Class.Name AS Classname , 
     Students.Name AS Studentname , 
     E.Student , 
     E.Class , 
     E.[Month] , 
     E.TotalMarks , 
     E.ObtainedMarks 
FROM Class 
     INNER JOIN Examination E ON Class.ID = E.Class 
     INNER JOIN Months ON E.[Month] = Months.ID 
     INNER JOIN Students ON E.Student = Students.ID 
GROUP BY Class.Name , 
     Months.Name , 
     Students.Name , 
     E.Student , 
     E.Class , 
     E.[Month] , 
     E.TotalMarks , 
     E.ObtainedMarks 
ORDER BY Obtained DESC 
+1

集合函數呢? – gvee

+0

這是一個無效的使用'GROUP BY'運營商,並會產生錯誤信息與任何符合標準的SQL數據庫 –

+0

此查詢顯示錯誤:消息8120,級別16,狀態1,行1 列「Months.Name」在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。 –