How do I get this to work, it works without the Where Clause, otherwise with the Where clause, i get the obvious error, but that's basically what needs to be done, anyone know how to approach this?
select ID,
Name,
case T.N
when 1 then City1
when 2 then City2
when 3 then City3
end as City,
case T.N
when 1 then State1
when 2 then State2
when 3 then State3
end as State
from YourTable
cross join (values(1),(2),(3)) as T(N)
Where City is NOT Null
You can't use the alias in the WHERE clause. Either repeat the expression (messy) or else put your SELECT in a subquery and then put the WHERE clause in the outer query:
You can't use an alias (from
SELECT
clause) inWHERE
clause because the logical processing order(section:Logical Processing Order of the SELECT statement
) isWHERE
and thenSELECT
:But, you can use an alias in
ORDER BY
:Solutions: see the solutions presented by Mark Byers.
Tibor Karaszi: Why can't we have column alias in ORDER BY?