I'm trying to execute a SQL query which requires grouping by MAX of SUM calculation (in PostgreSQL).
I found here some solutions which uses sub-querying but I need the solution without it (if it's possible).
Query:
SELECT "Festival".title,
"Musician".aname,
SUM("Musician".salary * "Musician".percentage / 100) AS "agent_total_profit"
FROM "Festival"
INNER JOIN "Booked"
ON "Booked".title = "Festival".title
INNER JOIN "Musician"
ON "Musician".id = "Booked".id
GROUP BY "Festival".title,
"Musician".aname
ORDER BY "Festival".title
Result:
the result is not as expected, I want to find for each festival title, the musician aname with the max agent_total_profit.
Desired result:
Thanks in advance.