Wednesday, March 21, 2012

Referensing an alias field within the same view

I have created an Alias field in a View using a Case statement and next I
want to reference that Alias field in another Alias field with a Case
statement. I get an error stating the first field is not valid. Example of
what I'm trying to do
Column
Alias
CASE WHEN [A] = 0 AND [B] = 1 THEN 1 ELSE 0 END Expr1
CASE WHEN [Expr1] = 1 AND [C] = 1 THEN 1 ELSE 0 END Expr2
SQL doesn't like my referencing Expr1 in the second field. I suppose I could
save the first view and then create a new view based on the first but I was
hoping there might be a way to get around that. Thanks for any help.AkAlan,
As you said, it is an alias and you can not reference it in the same column
list. May be using a derived table, a view, or rewiting the expression.
select
orderid, productid, ext_price * (1.00 - (discount / 100.00)) as exp2
from
(
select orderid, productid, quantity * unitprice as ext_price
from [order details]
) as t
go
AMB
"AkAlan" wrote:

> I have created an Alias field in a View using a Case statement and next I
> want to reference that Alias field in another Alias field with a Case
> statement. I get an error stating the first field is not valid. Example of
> what I'm trying to do
> Column
> Alias
> CASE WHEN [A] = 0 AND [B] = 1 THEN 1 ELSE 0 END Expr1
> CASE WHEN [Expr1] = 1 AND [C] = 1 THEN 1 ELSE 0 END Expr2
> SQL doesn't like my referencing Expr1 in the second field. I suppose I cou
ld
> save the first view and then create a new view based on the first but I wa
s
> hoping there might be a way to get around that. Thanks for any help.
>

No comments:

Post a Comment