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)
 calculated column name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eevans
Starting Member

48 Posts

Posted - 03/03/2009 :  14:02:59  Show Profile  Reply with Quote
Is it possible to reference a calculated column in a query elsewhere within that same query. I tried using the calculated column's assigned name and but it doesn't seem to work. Thanks.

yosiasz
Flowing Fount of Yak Knowledge

USA
1635 Posts

Posted - 03/03/2009 :  14:08:33  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
how are you doing it? can you show your query? is it from the same table or two different tables?
show us tables involved and structure.
u can make it a subquery
Select SumBreakMinutes, (select SUM(BreakMinutes) SumBreakMinutes from VBreaks vb where vb.ShiftID = vs.ShiftID)
     From Vshifts vs

somthing like that I believe
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 03/03/2009 :  14:11:28  Show Profile  Reply with Quote
Yeah, I thought about doing a subquery. I was hoping their was a simplier way. Thanks.
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 03/03/2009 :  14:45:23  Show Profile  Reply with Quote
No, you can't refer to a column alias from the same statement that defines it. Unless:

If it is in the ORDER BY clause then you can use an INT to refer to the column position.
ie: ORDER BY 1,2,3
will order by the first, second and third columns returned even if one of the values is the result of an expression.

If it is any place other than the ORDER BY clause then you can't refer to the column alias. Ways around would be to use a Derived table or Common Table Expression to encapsulate the statement that defines the column alias, then you can refer to it as a column in outer statements.

EDIT:
a typical solution is to repeat the expression rather than refer to its alias.

Be One with the Optimizer
TG

Edited by - TG on 03/03/2009 14:46:44
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 03/03/2009 :  15:18:53  Show Profile  Reply with Quote
Thanks.
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.05 seconds. Powered By: Snitz Forums 2000