Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Aliases in Where Clause or an Alternative Option?

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
like image 870
Control Freak Avatar asked Oct 09 '11 18:10

Control Freak


People also ask

Can you use aliases in WHERE clause?

The WHERE clause can contain non-correlated aliases and correlated aliases.

Can you use alias in WHERE clause SQL Server?

In PROC SQL, a column alias can be used in a WHERE clause, ON clause, GROUP BY clause, HAVING clause, or ORDER BY clause. In the ANSI SQL standard and ISO SQL standard, the value that is associated with a column alias does not need to be available until the ORDER BY clause is executed.

Can we use alias in WHERE clause in mysql?

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

What can aliases be used for?

An email alias is an additional email address for an email account, with which a user can send/ receive emails or set forwards to. A single user account can have multiple email aliases, with different domains or even with the same domain. In short, an email alias is like a nick-name or a nick email address.


2 Answers

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:

SELECT Id, Name, City, State
FROM
(
     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)
) T1
WHERE City IS NOT NULL
like image 117
Mark Byers Avatar answered Oct 17 '22 12:10

Mark Byers


You can't use an alias (from SELECT clause) in WHERE clause because the logical processing order(section: Logical Processing Order of the SELECT statement) is WHERE and then SELECT:

FROM    
ON
JOIN
WHERE <--
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT <--
DISTINCT
ORDER BY <--
TOP

But, you can use an alias in ORDER BY:

SELECT  h.SalesOrderID, YEAR(h.OrderDate) OrderYear
FROM    Sales.SalesOrderHeader h
ORDER BY OrderYear;

Solutions: see the solutions presented by Mark Byers.

Tibor Karaszi: Why can't we have column alias in ORDER BY?

like image 41
Bogdan Sahlean Avatar answered Oct 17 '22 11:10

Bogdan Sahlean