Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To apply an aggregate function on an unnested array in Postgres, you can use the unnest() function to convert the array elements into individual rows, and then apply the aggregate function.

For example, if you have a table "students" with a column "grades" that contains arrays of grades for each student, and you want to calculate the average grade for all students:

SELECT AVG(grade) FROM unnest(students.grades) AS grade;

Here, the unnest() function converts the array of grades for each student into individual rows, and then the AVG() function is applied on the resulting set of grades to calculate the average grade for all students.