Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql AVG的AVG,子查询的聚合函数_Sql_Postgresql_Aggregate_Average_Derived - Fatal编程技术网

Sql AVG的AVG,子查询的聚合函数

Sql AVG的AVG,子查询的聚合函数,sql,postgresql,aggregate,average,derived,Sql,Postgresql,Aggregate,Average,Derived,此子查询生成正确的表。但是现在我想得到平均值的平均值,我得到一个错误“Missing FROM子句条目for table“c” 您正在命名ProductAverages,因此表别名应该引用它,而不是c——它只能在内部查询中使用: SELECT name, -- Here AVG(avgvalue) FROM ( SELECT c.name, p.name, AVG(a."value") AS avgvalue FROM answers a INNER JOIN surv

此子查询生成正确的表。但是现在我想得到平均值的平均值,我得到一个错误“Missing FROM子句条目for table“c”


您正在命名
ProductAverages
,因此表别名应该引用它,而不是
c
——它只能在内部查询中使用:

SELECT
   name, -- Here
   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') 
GROUP BY c.name, p."name"
HAVING count(p.name)>10
) as ProductAverages
GROUP BY name; -- and here

哦,明白了。谢谢
SELECT
   name, -- Here
   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') 
GROUP BY c.name, p."name"
HAVING count(p.name)>10
) as ProductAverages
GROUP BY name; -- and here