SQL嵌套聚合函数作为子查询语法错误

时间:2015-01-29 20:44:30

标签: sql postgresql subquery average derived

我需要执行平均值。弄清楚如何编写子查询,但最终函数抛出两个错误。第15行然后第1行语法错误。

子查询有效。然后我只需要平均同一类别的产品平均值。缺少什么?

SELECT
   c."name",
   AVG(avgvalue)
FROM
   (SELECT
       c.name, 
       p.name,
       AVG(a."value") AS avgvalue
   FROM 
       answers a 
   INNER JOIN 
       survey_responses sr ON sr.id = a.survey_response_id 
                           AND a.question_id = 13
   INNER JOIN 
       answers category_answer ON category_answer.survey_response_id = sr.id 
                               AND category_answer.question_id = 264
   INNER JOIN 
       answers_categories ac ON category_answer.id = ac.answer_id
   INNER JOIN 
       categories c ON c.id = ac.category_id
   INNER JOIN 
       products p ON p.id = a.product_id
   WHERE 
       c.name IN ('Accounting') 
   HAVING 
       count(p.name) > 10) AS ProductAverages
GROUP BY c.NAME

1 个答案:

答案 0 :(得分:1)

在临时表

中的;子句后删除HAVING
HAVING count(p.name)>10