postgresql – PGError:错误:列“p.name”必须出现在GROUP BY子句中或用于聚合函数

前端之家收集整理的这篇文章主要介绍了postgresql – 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
将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.

猜你在找的Postgre SQL相关文章