How to fix Aggregate function calls cannot be nested in PostgreSQL | DevErrors