sqlsql-servercolumn-alias

Deriving new column using new column


Normally we can build new columns in Select statements. Is it possible to derive new columns out of new columns in Select statements?

For example,

Select 1+2 A

This gives us a table with column A and the value 3 in 1 row.

Can I build a new column B that is based on A?

For example,

Select 1+2 A, 4+A B

Unfortunately the above will result in "Invalid column name A".


Solution

  • Use a subquery.

    select 4 + A as 'B'
    from (
        select 1 + 2 as 'A'
    ) q