如何向SQL Group By子句添加其他字段

时间:2010-08-19 18:40:23

标签: sql ms-access

我有以下MS-Access SQL表: -

NAME,SUBJECT,SCORE

..我需要找到平均分数,最高分和最高分数的主题。我设法生成以下查询但不确定如何合并SUBJECT字段: -

SELECT  NAME,  Avg(SCORE) AS AverageScore, MAX(SCORE) AS best_score
FROM Scoretable
GROUP BY NAME

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

select sm.NAME, sm.AverageScore, sm.best_score, s.SUBJECT
from (
    SELECT  NAME,  Avg(SCORE) AS AverageScore, MAX(SCORE) AS best_score 
    FROM Scoretable 
    GROUP BY NAME 
) sm
inner join Scoretable s on sm.NAME = s.NAME 
    and sm.best_score  = s.SCORE