PostgreSQL视图:引用另一个计算字段中的一个计算字段

时间:2010-03-05 09:52:23

标签: sql postgresql calculated-columns sql-view

我和#1895500有同样的问题,但PostgreSQL不是MySQL。

如何定义具有计算字段的视图,例如:

 (mytable.col1 * 2) AS times_two

...并创建另一个基于第一个的计算字段:

 (times_two * 2) AS times_four

...

2 个答案:

答案 0 :(得分:6)

根据公式的重量,您可以使用子查询:

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

或重写计算:

select mycol * 2, mycol * 2 * 2 from table

答案 1 :(得分:-1)

  

使用此声明

 
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 ;