postgresql - 列必须出现在 GROUP BY 子句中

标签 postgresql

我有这个查询:

SELECT 
"EventReadingListItem"."id"
, "EventReadingListItem"."UserId"
FROM "EventReadingListItems" AS "EventReadingListItem" 
group by "EventReadingListItem"."EventId";

当我运行它时,我收到错误

Column "EventReadingListItem"."id" must appear in the GROUP BY clause or be used in an aggregate function.
为什么?我读过类似的问题,但我真的不明白为什么这个简单的分组不起作用。是因为 group by 中的字段还不被称为“EventReadingListItem”吗?

最佳答案

所以,根据您的评论,这应该适合您。

为每个具有最小/分钟 id 值的 EventId 提供唯一行:

select DISTINCT ON (EventId) EventId, id, UserId
from EventReadingListItems
order by EventId, id

关于postgresql - 列必须出现在 GROUP BY 子句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900414/

相关文章:

postgresql - 返回 PostgreSQL 中更新值的变化

sql - POSTGRESQL COUNT() 返回更多

postgresql - postgreSQL 上的时间戳到 unix 时间戳

sql - 在 where 子句中使用 "AND"时出现错误 "argument must be type boolean"

sql - 将 ActiveRecord 查询重写为递归 SQL

PostgreSQL + ANALYZE + pg_statistic 表

sql - For循环遍历临时表

sql - 在 PostgreSQL 中删除带有外键的行

mysql - SQL:count() 的用法 - 困惑和解释

sql - 查询将订单存储为 postgresql 中的值