2011-05-02 51 views
1

我有以下表格:平均多行中訪問

 
Table 1 
Student , Exam_ID 
1  1  
2  1  
3  2  
1  2  
3  3  
2  3  
3  4  
1  4 

Table 2 
Exam ID, Mark 
(1 , 5) 
(2 , 4) 
(3 , 4) 
(4 , 5) 

每次考試是由對學生的解決...我希望能夠以平均每個採取的所有考試的標誌例如一對學生:考試2和4由同一對學生(3,1)拍攝,我希望能夠對那些(4,5)= 4.5 這兩項考試進行平均分,然後對這些分數進行排名從最高到最低的分數對 謝謝

如何將First_Name和Surname添加到第一個表中?

回答

0
SELECT 
    a.Student AS studentA 
    , b.Student AS studentB 
    , AVG(T2.Mark) AS averageMark 
FROM (T2 
     INNER JOIN T1 AS a 
      ON a.Exam_ID = T2.Exam_ID 
    ) 
     INNER JOIN T1 AS b 
      ON a.Exam_ID = b.Exam_ID 
      AND a.Student < b.Student 
GROUP BY a.Student 
     , b.Student 
ORDER BY AVG(T2.Mark) DESC 

或本:

SELECT 
    a.Student AS studentA 
    , b.Student AS studentB 
    , AVG(T2.Mark) AS averageMark 
FROM (T1 AS a 
     INNER JOIN T1 AS b 
      ON a.Exam_ID = b.Exam_ID 
      AND a.Student < b.Student 
    ) 
     INNER JOIN T2 
      ON a.Exam_ID = T2.Exam_ID 
GROUP BY a.Student 
     , b.Student 
ORDER BY AVG(T2.Mark) DESC 

它更明顯它是如何工作的。圓括號內的JOIN發現夫婦和下一個JOIN將夫妻關聯到第二個Marks表。

+0

它沒有工作.. – Tantoon 2011-05-02 22:48:11

+0

你是什麼意思?它給了什麼錯誤? – 2011-05-02 22:52:10

+0

它表示select中的單詞被保留或拼寫錯誤n突出顯示b。學生 – Tantoon 2011-05-02 22:54:02