Understanding Oracle aliasing - why isn't an alias not recognized in a query unless wrapped in a second query?

后端 未结 4 1220
我寻月下人不归
我寻月下人不归 2020-12-07 01:38

I have a query


SELECT COUNT(*) AS \"CNT\",
       imei
FROM   devices  

which executes just fine. I want to further restrict the query wi

4条回答
  •  北海茫月
    2020-12-07 02:25

    To summarize, this little gem explains:

    10 Easy Steps to a Complete Understanding of SQL

    A common source of confusion is the simple fact that SQL syntax elements are not ordered in the way they are executed. The lexical ordering is:

    SELECT [ DISTINCT ]
    FROM
    WHERE
    GROUP BY
    HAVING
    UNION
    ORDER BY
    

    For simplicity, not all SQL clauses are listed. This lexical ordering differs fundamentally from the logical order, i.e. from the order of execution:

    FROM
    WHERE
    GROUP BY
    HAVING
    SELECT
    DISTINCT
    UNION
    ORDER BY
    

    As a consequence, anything that you label using "AS" will only be available once the WHERE, HAVING and GROUP BY have already been performed.

提交回复
热议问题