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
 One Calculation For query

Author  Topic 

Alaveh
Starting Member

24 Posts

Posted - 2011-10-25 : 10:18:30
Hi

i need to calculate a column value once and use it in more than one column . like this :

select F1,F2,((F1+(F2*F2*F2))/2) as F3,(F3+f4) as F5

How can i ? what is the syntax ?

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-10-25 : 11:50:29
Well you could do it using a derived query . .

create table #test (
f1 int,
f2 int,
f4 int)

insert into #test
select 1,2,4

SELECT f1,f2,f3,(F3+f4) as F5
from (
select F1,F2,((F1+(F2*F2*F2))/2) as F3, f4
from #test
) d


But to be honest you'd probably be better just rewriting the calculation in the calc for F5 e.g.
select F1,F2,((F1+(F2*F2*F2))/2) as F3,(((F1+(F2*F2*F2))/2)+f4) as F5

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

Alaveh
Starting Member

24 Posts

Posted - 2011-10-25 : 11:58:55
thank you theboyholty
Go to Top of Page
   

- Advertisement -