PGError:错误:列“p.name”必须出现在 GROUP BY 子句中或在聚合函数中使用
我收到此查询错误。为什么?我不明白 :(
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
I get an error with this query. Why? I don't understand :(
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
GROUP BY project_id
更改为GROUP BY p.name,p.id
文档 说:
Change
GROUP BY project_id
toGROUP BY p.name,p.id
The docs say: