Search code examples
mysqlsqlsql-servercode-reuse

re use existing selected column in sql server?


Say I have a query that goes like this

select column1
,column2
,column3
,column4
,column5 + reusethiscolumn as Reusethiscolumn2

[can i use here the data that will be derived from the column below?]

,case[computation here] as reusethiscolumn
from table

is there any way to do this? thanks in advance.


Solution

  • select 
    column1
    ,column2
    ,column3
    ,column4
    ,(column5 + reusethiscolumn) as Reusethiscolumn2
    from
    (
      select 
        column1
       ,column2
       ,column3
       ,column4
       ,column5
       ,case[computation here] as reusethiscolumn
      from table
    ) AS SubQ