Ask Your Question
3

How to apply an Aggregate Function on an Unnested Array in Postgres?

asked 2023-06-28 12:40:52 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-28 12:43:02 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-28 12:40:52 +0000

Seen: 21 times

Last updated: Jun 28 '23