I wrote a query as below, and the result shows: ERROR: syntax error at or near "rank" Position: 58. I don't know why it is wrong and I guess that is because aggregate not allowed to work on alias"hours". Can someone help me clarify this question? I appreciate it.
SELECT
firstname,
surname,
(sum(bks.slots * 0.5)) AS hours rank(hours) DESC AS rank
FROM
cd.members mem
INNER JOIN cd.bookings bks ON mem.memid = bks.memid
GROUP BY
firstname,
surname
ORDER BY
hours DESC;