user1882338
user1882338

Reputation:

Using computed column more than once

Is there any way to use a computed column more than once, for example:

SELECT col1 / 1.1 as col2, col2 - col3 as col4 FROM table1

Thanks in advance.

Upvotes: 0

Views: 53

Answers (2)

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

You can also use Common Table Expression

WITH CTE
AS
(
     SELECT col1 / 1.1 as col2, 
            col3
     FROM table1
)

SELECT col2, 
       col2 - col3 as col4 
FROM CTE 

Upvotes: 0

Lamak
Lamak

Reputation: 70638

Not like that. You can use it on a derived table:

SELECT col2, col2-col3 col4
FROM (SELECT col1/1.1 col2, col3 FROM table1) A

You can also use CROSS APPLY:

SELECT col2,
       col2-col3 col4
FROM table1
CROSS APPLY (SELECT col1/1.1) A(col2)  

Upvotes: 2

Related Questions