tblStudent
studentid sex
------------------
1 female
2 male
tblGrade
studentid subject Score
--------------------------
1 math 10
2 math 30
1 english 40
2 english 30
1 sci 20
2 sci 50
Access查询如何查看,计算男性和女性在数学和科幻分数上的差异?
答案 0 :(得分:1)
您可以对subject
进行分组,并使用AVG
汇总函数作为平均值。
SELECT
g.subject,
AVG(IIf(s.sex='male', g.score, null)) AS avg_male,
AVG(IIf(s.sex='female', g.score, null)) AS avg_female,
AVG(IIf(s.sex='male', g.score, null)) -
AVG(IIf(s.sex='female', g.score, null)) AS avg_diff
FROM
tblStudent s
INNER JOIN tblGrade g
ON s.studentid = g.studentid
WHERE g.subject IN ('math', 'sci')
GROUP BY g.subject
请注意,Access在平均计算中不包含NULL字段。因此,当性别不是我们需要的时候,我们可以使用NULL。