SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to reference new column in select statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

timlisten
Starting Member

USA
26 Posts

Posted - 06/16/2012 :  15:22:37  Show Profile  Reply with Quote
Hi, Let's say I have a table with col1 and col2 and a statement like the this

select (col1+col2) as col_new, (col_new+col1) as col_new2,....
FROM table

why the sql cannot recognized the newly computed col_new? I believe in mysql that works fine. In my situation, the col_new is much more complicated than the simple col1+col2, but I want to reuse that as a standing point instead of recalculating for every new column. Is there a direct way of doing this without using derive query because I have many calculated columns on the fly?

Edited by - timlisten on 06/16/2012 15:30:44

visakh16
Very Important crosS Applying yaK Herder

India
47065 Posts

Posted - 06/17/2012 :  11:47:26  Show Profile  Reply with Quote

select col_new,(col_new+col1) as col_new2,
from
(
select (col1+col2) as col_new, ....
FROM table
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

timlisten
Starting Member

USA
26 Posts

Posted - 06/18/2012 :  13:57:16  Show Profile  Reply with Quote
Hi visakh16, I understand you can do that, but the problem is I have too many computations that I need to do. Each derive from the other, if I have to add a new layer to each the code just becomes very confusing.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47065 Posts

Posted - 06/18/2012 :  14:50:59  Show Profile  Reply with Quote
quote:
Originally posted by timlisten

Hi visakh16, I understand you can do that, but the problem is I have too many computations that I need to do. Each derive from the other, if I have to add a new layer to each the code just becomes very confusing.


you've to either add a new layer or repeat calculation as it is. You cant use aliases directly in same level

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

timlisten
Starting Member

USA
26 Posts

Posted - 06/18/2012 :  20:50:28  Show Profile  Reply with Quote
thanks, I guess I will have to do it this way if there is no other way around.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47065 Posts

Posted - 06/18/2012 :  22:48:58  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000