max - SQL的平均值

时间:2014-02-05 12:21:59

标签: sql syntax-error aggregate-functions

我试图为每个没有接受答案的StackOverflow问题获得最高投票答案的平均分数。

使用以下查询:

SELECT AVG(MaxScore) 
FROM
    (SELECT MAX(a.Score) as MaxScore
     FROM Posts a
     JOIN Posts q ON q.Id = a.ParentID
     WHERE q.PostTypeId = 1 AND q.AcceptedAnswerId IS NULL
     GROUP BY q.Id);

只给了我:"'''"

附近的语法不正确

任何想法为什么? 谢谢!

1 个答案:

答案 0 :(得分:4)

SELECT AVG(MaxScore) 
FROM
    (SELECT MAX(a.Score) as MaxScore
     FROM Posts a
     JOIN Posts q ON q.Id = a.ParentID
     WHERE q.PostTypeId = 1 AND q.AcceptedAnswerId IS NULL
     GROUP BY q.Id) AS sub;

可能需要这样的子查询的名称,具体取决于数据库系统,即使您不使用它。