Using Aliases in Where Clause or an Alternative Option?

前端 未结 2 1912
你的背包
你的背包 2020-11-30 11:04

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 k

2条回答
  •  时光说笑
    2020-11-30 11:51

    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?

提交回复
热议问题