Skip to content
Advertisement

Nested aggregate function in PostgreSQL

I am trying to return every author_id, author_name, and AVG(total) per author for every article_group. I am trying to lump the author_id, author_name, and AVG(total) into arrays. I understand that this query will return an article_group per array, but that is fine.

I originally tried putting AVG(total) (instead of avg_total) inside my array_agg(). This resulted in an error message stating that I cannot have nested aggregate functions.

I have been trying to figure out a work around and can’t seem to figure one out. I tried putting a subquery in WHERE clause AS avg_total and that didn’t work.

So now I tried putting the AS avg_total alias in an independent subquery before the FROM clause and it is still not working.

Here is the query:

And this is the current error message:

Here are my tables:

Advertisement

Answer

In your question so many things are unclear. Based on what i understood from your current query, try this:

You can change whatever you want in array_agg

DEMO

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement