在Access中的列中AVERAGE多行

时间:2011-05-02 22:00:07

标签: sql ms-access

我有以下表格:

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的2项考试的分数 然后将这些对从最高分到最低分排名 谢谢

如何将First_Name和Surname包含在第一个表中?

1 个答案:

答案 0 :(得分: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表联系起来。