'Find max value in a column
I created a few databases and now trying to write a request to find events name, their organizers and the number of sold tickets (There are tables "Organizers", "Events", "Tickets"):
SELECT "Organizers".name, "Events".name, COUNT("Tickets".ID) as tickets_count FROM "Events"
INNER JOIN "Organizers" on "Events".organizer_ID = "Organizers".ID
INNER JOIN "Tickets" on "Events".ID = "Tickets".event_ID
GROUP BY "Organizers".name, "Events".name;
I've got this result:
But I don't know how to output only events with max tickets_count. Does somebody know how to do it?
Solution 1:[1]
In recent (starting with v13) versions of PostgreSQL, just add to the end
order by tickets_count DESC fetch first 1 row with ties;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jjanes |

