I am following along the example of this excellent question and first answer:
PostgreSQL - fetch the rows which have the Max value for a column in each GROUP BY group
But I need to do something slightly different. I want to select distinctly based on events.uuid, because events.start might not be unique. But I want to order by events.start because uuid's are generated in no particular order. Database doesn't like it. What is a good way of doing what I really want to do?
SELECT DISTINCT ON (events.uuid) events.nickname, plays.id
FROM events
JOIN plays on plays.eventuuid = events.uuid
ORDER BY events.start;
In English I would state it like this: "For each event, give me exactly one play, and order the results by event start time."