oderfla
oderfla

Reputation: 1797

Column must appear in the GROUP BY clause

I have this query:

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

When I run it I get the error

Column "EventReadingListItem"."id" must appear in the GROUP BY clause or be used in an aggregate function.

Why? I have read similar questions but I don't really get why this simple group by is not working. Is it because the field in group by is not known as "EventReadingListItem" yet?

Upvotes: 2

Views: 5888

Answers (1)

Oto Shavadze
Oto Shavadze

Reputation: 42753

So, according to your comment, this should work for you.

Gives unique rows for each EventId which does have smallest/min id value:

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

Upvotes: 5

Related Questions