Home:ALL Converter>PostgreSQL Views: Referencing one calculated field in another calculated field

PostgreSQL Views: Referencing one calculated field in another calculated field

Ask Time:2010-03-05T17:52:23         Author:John

Json Formatter

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

...?

Author:John,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2385791/postgresql-views-referencing-one-calculated-field-in-another-calculated-field
Konrad Garus :

Depending on how heavy the formla is, you could use a subquery:\n\nselect inner.*, times_two * 2 from\n(select mycol * 2 as times_two from table) sub\n\n\nOr rewrite the computation:\n\nselect mycol * 2, mycol * 2 * 2 from table\n",
2010-03-05T09:56:42
yy