How to create this view in postgres?

1
CREATE VIEW grafico AS SELECT AVG(realizacao) , AVG(estima), AVG(fisiologica),AVG(seguranca),AVG(social) FROM necessidade;

I'm trying to create a view that brings the average of five columns of the required table, but I can not create this view with the above query.

Postgres says that I'm using the AVG function more than once.

Follow my table need:

CREATE TABLE necessidade
(
  id serial NOT NULL,
  realizacao integer,
  estima integer,
  fisioologica integer,
  seguranca integer,
  social integer,
  PRIMARY KEY (id)
)
    
asked by anonymous 26.03.2018 / 20:29

1 answer

0

I simulated a query here and did not check any impediment with the number of times the function is used. Perhaps by showing a little more of the data you are using if you can better analyze the query.

So it goes wrong:

create or replace view teste as
select avg(x.col1), avg(x.col2), avg(x.col3)

This is correct:

create or replace view teste as
select avg(x.col1) as avgcol1, avg(x.col2) as avgcol2, avg(x.col3) as avgcol3
from
(
select 13 as col1, 19 as col2, 44 as col3
union all
select 14 as col1, 39 as col2, 12 as col3
union all
select 23 as col1, 11 as col2, 10 as col3
union all
select 43 as col1, 79 as col2, 9 as col3) as x
    
26.03.2018 / 20:40