Skip to content
Advertisement

How to get the average from computed columns in Postgresql?

I have a query:

How to get an average data from sum_1, sum_2, sum_3? If I write a query in the next way:

I got an Error:

[42703] ERROR: column “sum_1” does not exist

Advertisement

Answer

You seem to want to compute the average of the three columns on the same row. If so, you don’t need avg() (that is an aggregate function, that operates across rows). Instead, you can use regular artihmetics:

The subquery is not striclty necessary. You could also repeat the sum()s, like:

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