sqlpostgresqlpostgresql-9.3

How can I create a temporary column as the result of arithmetic from two other columns?


I want to achieve the following:

select a, 
       b,
       diff as (b-a)
from some_table;

Solution

  • Your syntax isn't far off, try this:

    select a, 
           b,
           b - a as diff
    from some_table;
    

    The general ANSI syntax for aliasing a column is:

    select <expression involving columns> as <alias name>
    from some_table