gpt4 book ai didi

postgresql - PGError : ERROR: column "p.name" must appear in the GROUP BY clause or be used in an aggregate function

转载 作者:行者123 更新时间:2023-11-29 11:38:33 29 4
gpt4 key购买 nike

我在查询时遇到错误。为什么?我不明白:(

    SELECT p.name, p.id, SUM(hours) AS hours, SUM(logged_hours) AS logged_hours
FROM (
SELECT project_id, date, hours, null AS logged_hours
FROM #{ScheduleEntry.table_name}
WHERE user_id = #{User.current.id}
AND date BETWEEN '%s' AND '%s'
UNION
SELECT project_id, spent_on AS date, null AS hours, sum(#{TimeEntry.table_name}.hours) AS logged_hours
FROM #{TimeEntry.table_name}
WHERE user_id = #{User.current.id}
AND spent_on BETWEEN '%s' AND '%s'
GROUP BY project_id, date
) AS results
LEFT JOIN #{Project.table_name} AS p ON p.id = results.project_id
GROUP BY project_id

最佳答案

GROUP BY project_id 更改为 GROUP BY p.name,p.id

The docs说:

When GROUP BY is present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions, since there would be more than one possible value to return for an ungrouped column.

关于postgresql - PGError : ERROR: column "p.name" must appear in the GROUP BY clause or be used in an aggregate function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4786768/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com