I am using a table that looks like this:
userID, eventDate, eventName
1 2019-01-01 buySoup
2 2019-01-01 buyEggs
2 2019-01-03 buyMilk
2 2019-01-04 buyMilk
3 2019-01-02 buyBread
3 2019-01-03 buyBread
My current query is:
SELECT
userID,
FIRST_VALUE(eventName) OVER (
PARTITION BY userID ORDER BY eventDate ASC
) AS firstBought
FROM table
GROUP BY userID
I feel like this should return:
userID, firstBought
1 buySoup
2 buyEggs
3 buyBread
Instead, it gives the error:
'ERROR: Column "table.eventName" must appear in the GROUP BY clause or be used in an aggregate function'
Is there a way to grab this value without including it in the group by function, or creating a sub query? I'm using PostgreSQL.
If I do include it in the group by clause, it returns
userID, firstBought
1 buySoup
2 buyEggs
2 buyEggs
2 buyEggs
3 buyBread
3 buyBread
I understand that I could make it a subquery and then group by userID, firstBought, but I'd rather not create another subquery.