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.
Author |
Topic |
eevans
Starting Member
48 Posts |
Posted - 2009-03-03 : 14:02:59
|
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
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-03-03 : 14:08:33
|
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 subquerySelect SumBreakMinutes, (select SUM(BreakMinutes) SumBreakMinutes from VBreaks vb where vb.ShiftID = vs.ShiftID) From Vshifts vs somthing like that I believe |
|
|
eevans
Starting Member
48 Posts |
Posted - 2009-03-03 : 14:11:28
|
Yeah, I thought about doing a subquery. I was hoping their was a simplier way. Thanks. |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-03 : 14:45:23
|
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 OptimizerTG |
|
|
eevans
Starting Member
48 Posts |
Posted - 2009-03-03 : 15:18:53
|
Thanks. |
|
|
|
|
|