How can I use a calculated column multiple times in the same select without repeating the expression and without using common table expressions or complex subselects?
DECLARE @T TABLE ( NUM1 INT,NUM2 INT)
INSERT INTO @T VALUES (2,3);
INSERT INTO @T VALUES (5,7);
INSERT INTO @T VALUES(32,3);
INSERT INTO @T VALUES(6,8);
SELECT (NUM1+NUM2) [ADD], [ADD]*2, [ADD]/2,* FROM @T
Is there any way to solve this in SQL Server 2005?
You can use cross apply
or a CTE
or a subquery (also known as a derived table)
It is not possible to use the column alias in the same field list as it is declared.
You could use a derived table to accomplish this: