AVERAGE multiple rows in a column in Access

耗尽温柔 提交于 2019-12-24 06:44:51

问题


I have the following tables:

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)

each exam is solved by pairs of students ... i want to be able to average the mark of all exams taken by each pair of student for example : Exams 2 and 4 are taken by the same pair of students (3,1) i want to be able to average the marks for those 2 exams which are(4,5)=4.5 and then rank those pairs from highest to lowest marks thank you

How can I include First_Name and Surname into the first table?


回答1:


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

or this:

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

where it is more obvious how it works. The JOIN inside the parenthesis finds the couples and next JOIN relates the couples to the second Marks table.



来源:https://stackoverflow.com/questions/5862953/average-multiple-rows-in-a-column-in-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!