7

I have the same question as #1895500, but with PostgreSQL not MySQL.

How can I define a view that has a calculated field, for example:

 (mytable.col1 * 2) AS times_two

... and create another calculated field that's based on the first one:

 (times_two * 2) AS times_four

...?

Community
  • 1
  • 1
John
  • 267
  • 2
  • 4
  • 9
  • See also: https://stackoverflow.com/questions/8840228/postgresql-using-a-calculated-column-in-the-same-query – Peter Berg Jun 22 '18 at 14:09

2 Answers2

11

Depending on how heavy the formla is, you could use a subquery:

select inner.*, times_two * 2 from
(select mycol * 2 as times_two from table) sub

Or rewrite the computation:

select mycol * 2, mycol * 2 * 2 from table
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
-2

Use this statement

 
CREATE  VIEW  view_name as  SELECT  column_name*2 as new_col1 , column_name*4 as new_col2  from table_name ; 

select * from view_name ; 

If you want use this view column values. use following things 

create view new_viwe as select new_col1*2 as final_column from view_name ; 

select * from new_view ; 


Pavunkumar
  • 5,147
  • 14
  • 43
  • 69