2011-10-11 76 views
0

這是我在過去幾天中必須處理的另一個問題。我有以下查詢:如何根據父查詢返回作爲單個結果返回的嵌套查詢的平均值

SELECT S.Name,S.Surname,S.Student_ID,S.StudentNumber,C.Course, B.Campus_Title,M.Module,SM.Percentage_Obtained,S.Days_Absent 
FROM Students S 
INNER JOIN Student_Courses SC 
ON SC.StudentID = S.ID 
INNER JOIN Courses_Template C 
ON C.ID = SC.courseID 
INNER JOIN Branches B 
ON B.ID = S.BranchID 
INNER JOIN Student_Modules SM ON 
SM.StudentID =S.ID 
INNER JOIN Modules_Template M ON 
M.ID = SM.ModuleID 

的問題是,我想重寫查詢來獲得這樣的計算結果:

SELECT S.Name,S.Surname,S.Student_ID,S.StudentNumber,C.Course, B.Campus_Title,M.Module,SM.Percentage_Obtained,S.Days_Absent, 
     (SELECT AVG(Percentage_Obtained) 
     FROM Student_Modules 
     INNER JOIN Courses_Template 
     ON Courses_Template.ID = Student_Modules.CourseID 
     INNER JOIN Modules_Template 
     ON Courses_Template.ID = Modules_Template.CourseID 
     WHERE Modules_Template.Module= M.Module)[AS Class Average] 
FROM Students S 
INNER JOIN Student_Courses SC 
ON SC.StudentID = S.ID 
INNER JOIN Courses_Template C 
ON C.ID = SC.courseID 
INNER JOIN Branches B 
ON B.ID = S.BranchID 
INNER JOIN Student_Modules SM ON 
SM.StudentID =S.ID 
INNER JOIN Modules_Template M ON 
M.ID = SM.ModuleID 

用於模塊的初始值是(提取)

M.Modules AVG(Percentage_Obtained) 

Module 1 99 
Module 2 98 
Module 3 94 
Module 1 94 
Module 2 22 
Module 3 100 

,我從第二個查詢想預期的結果是這樣的

M.Modules AVG(Percentage_Obtained) (SubQuery's Average) 
Module 1 99       97 
Module 2 98       60 
Module 3 94       96 
Module 1 94       97 
Module 2 22       60 
Module 3 100       96 

然而,它給我的是完全不同的東西

,我從第二個查詢想要的是這個

M.Modules AVG(Percentage_Obtained) (SubQuery's Average) 
Module 1 99       84 
Module 2 98       84 
Module 3 94       84 
Module 1 94       84 
Module 2 22       84 
Module 3 100       84 

任何人都可以或許從某處發現我的錯誤了預期的效果?我需要在單個查詢中使用所有這些字段,因爲我必須對其進行報告。子查詢的結果是至關重要的

它幾乎就像我需要這個(但動態的模塊的所有復發):

SELECT AVG(Percentage_Obtained) 
FROM Student_Modules 
INNER JOIN Modules_Template ON Modules_Template.ID = Student_Modules.ModuleID 
WHERE Modules_Template.Module = 'Module 1' -- And module 2, and module 3 .... but Dynamically 

回答

1

你可以試試下面的查詢:

SELECT S.Name,S.Surname,S.Student_ID,S.StudentNumber,C.Course, B.Campus_Title,M.Module,SM.Percentage_Obtained,S.Days_Absent, SM2.AvgPercentage_Obtained 
FROM Students S 
INNER JOIN Student_Courses SC 
ON SC.StudentID = S.ID 
INNER JOIN Courses_Template C 
ON C.ID = SC.courseID 
INNER JOIN Branches B 
ON B.ID = S.BranchID 
INNER JOIN Student_Modules SM ON 
SM.StudentID =S.ID 
INNER JOIN Modules_Template M ON 
M.ID = SM.ModuleID 
OUTER APPLY (SELECT AVG(Percentage_Obtained) AvgPercentage_Obtained 
      FROM Student_Modules 
      INNER JOIN Modules_Template ON Modules_Template.ID = Student_Modules.ModuleID 
      WHERE Student_Modules.Module = M.Module) SM2 
+0

如果我能我會親吻你! 非常感謝。你用那個救了我的皮。要研究外部應用盡快,不知道它 – Eon

+0

@EonRustedduPlessis - 有沒有必要這樣做;-)。我很高興答案奏效。 – Lamak

+0

它運作良好。只是,WHERE的最後一行Student_Modules.Module = M.Module應該是WHERE Student_Modules.ModuleID = M.ID – Eon