Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Use alias name as a column

Author  Topic 

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2014-02-25 : 06:39:09
I'm using this query to to calculate yearly finance values.

select [Year],[FinanceValue-2014],[FinanceValue-2013],[FinanceValue-2012],[FinanceValue- 2014]-[FinanceValue-2013] as [FinanceValue Variance]

Now I need to multiply the [FinanceValue Variance] * 2.50 and for that how can I use the alias name as column in the query. I tried this but it says invalid column name.

select [Year],[FinanceValue-2014],[FinanceValue-2013],[FinanceValue-2012],[FinanceValue- 2014]-[FinanceValue-2013] as [FinanceValue Variance], [FinanceValue Variance] * 2.50 as [NewVariance] from Finance

SumofVariance output will be like 5690.5893656 Also how can I show the SumofVariance to round off 4 decimal places like this 5690.5894. Really appreciate any help on this.

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-02-25 : 07:35:08
DECLARE @Table TABLE (Year INT,FinanceValue2005 INT,FinanceValue2004 INT,FinanceValue2003 INT)
INSERT INTO @Table VALUES(2011,100,90,80)

;With CTE([Year],FinanceValue2005,FinanceValue2004,FinanceValue2003,[FinanceValue Variance])
AS
(
SELECT *,FinanceValue2005 - FinanceValue2004 AS [FinanceValue Variance] FROM @Table
)
SELECT *,[FinanceValue Variance] * 2.50 AS [New Variance] FROM CTE


Veera
Go to Top of Page
   

- Advertisement -