I am learning SQL with PostgreSQL, and have run into a problem regarding nested Aggregate functions.
I am trying to find the details of private owners with the maximum amount of properties, where I have two relations, privateowner and propertyforrent with propertyforrent having a foreign key, ownwerno.
I suspect that my issue is where I am attempting to nest Aggregate Functions, but I cannot see a way around it.
NOTE:- the database I am using has a typo in the attribute ownwerno in propertyforrent, where it should be ownerno.
The code I am attempting to use is shown below~:-
SELECT o.fname, o.lname, telno
FROM privateowner o
WHERE o.ownerno = (SELECT p.ownwerno
FROM propertyforrent p
HAVING COUNT(p.ownwerno) = MAX(COUNT(o.ownerno)));
Its accompanying error is as follows:-
ERROR: column "p.ownwerno" must appear in the GROUP BY clause or be used in a
aggregate function
LINE 3: WHERE o.ownerno = (SELECT p.ownwerno
^
********** Error **********
ERROR: column "p.ownwerno" must appear in the GROUP BY clause or be used in an
aggregate function
SQL state: 42803
Character: 78
Any insight would be wonderful.