PSQL:"Must appear in the GROUP BY clause or be used in an aggregate function" 使用 JOIN 函数时出错

PSQL: "Must appear in the GROUP BY clause or be used in an aggregate function" error when using JOIN function

我正在尝试加入 table,但出现错误。

我需要提供一个子查询,但不知道在哪里。这是我试过的。

select q.id, q."createdAt", count(qq.id), "AccountId", "ProgramId" 
from "Quizzes" q 
join "QuizQuestions" qq on "QuizId" = q.id 
join "Accounts" a on a.id = "AccountId" 
where "ProductId" = 'abfb4db4-8605-46dd-a23d-ae52a28c1940' 
       and "selectedOptionIndex" is not null 
       and q."createdAt" >= '1-7-19' 
group by q.id;

结果:ERROR: column "a.ProgramId" must appear in the GROUP BY clause or be used in an aggregate function

所有非聚合列都应在 GROUP BY:

select q.id, q."createdAt", count(qq.id), "AccountId", "ProgramId"
from "Quizzes" q join
     "QuizQuestions" qq
     on qq."QuizId" = q.id join
     "Accounts" a
     on a.id = "AccountId"
where "ProductId" = 'abfb4db4-8605-46dd-a23d-ae52a28c1940' and
      "selectedOptionIndex" is not null and
       q."createdAt" >= '2017-01-17'
group by q.id, q."createdAt", "AccountId", "ProgramId";

我将日期格式更改为标准日期格式。您还应该使用它们的 table 别名为所有列引用设置别名。而且,所有的双引号都令人困惑。你真的需要它们吗?