How to create this view in postgres?

Asked

Viewed 1,231 times

1

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

I am trying to create a view that brings the average of five columns of the table needs, but I cannot create this view with the above query.

postgres says 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)
)

1 answer

0


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

so make a mistake:

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

that 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
  • thanks for the answer, I edited the question! I could explain this your answer better. I voted in your answer , it worked here!

  • @Penapintada, the alias x represents the query of your table "need", and select is the query that gives me the average result (avg) of the selected columns. Through this query, I created a view and there was no error. To answer the questions, create a view with this query I gave example and tell me the result.

  • I had already made a query like this a long time ago but I forgot what the query was like, thanks for the reply.

  • just to complement and this is your error, to generate the view you will have to add the column name. I edited the initial answer to be complete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.