Postgresql – Postgres : Multiply length of array to count aggregate result

aggregatepostgresql

Following command works fine :

select count(*) * 2 from rooms;

But if I am trying to do something like :

select count(*) * array_length(students,1) from rooms;

where students is an array column in the table rooms. Following error is thrown :

ERROR: column "rooms.students" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 32

What is the right way to get the desired result?

Best Answer

sum was an obvious choice :

select sum(array_length(students,1)) from rooms;