我有这个查询:
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/