I have the following query:
select FirstName, LastName,
Case
When LastName = 'Jones'
then 'N/A'
End as Other,
Case
When Other is not null
then 1
else 0 as Flag
The Flag value depends on Other but as Other is an alias, the Flag field does not recognize Other.
I guess, I can use a select within a select. Is there a better way for Flag to recognize the Other alias column?
You can’t refer to an alias outside of SELECT and ORDER BY because of the way a query is parsed. Typical workaround is to bury it in a derived table: