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
 Select A as B and Reference B in the where clause

Author  Topic 

emzzz
Starting Member

15 Posts

Posted - 2007-08-23 : 12:10:43
Hello,

When you rename a field/calculation in the select part of the sql statement, how do you reference it in the where clause?

For example:

Select A, B, (C + D) as X
From test
WHERE X > 1

Many thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-23 : 12:14:42
Select A, B, (C + D) as X
From test
WHERE (C + D) > 1

select a, b, x from (
Select A, B, (C + D) as X
From test
) as d
WHERE x > 1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-24 : 04:47:18
quote:
Originally posted by emzzz

Hello,

When you rename a field/calculation in the select part of the sql statement, how do you reference it in the where clause?

For example:

Select A, B, (C + D) as X
From test
WHERE X > 1

Many thanks!


Note that you cant use alias name directly in where or group by clause but you can use it only in order by clause

Select A, B, (C + D) as X
From test
order by X

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

emzzz
Starting Member

15 Posts

Posted - 2007-08-24 : 04:59:09
Many thanks!
Go to Top of Page
   

- Advertisement -