Using Alias In When Portion of a Case Statement in Oracle SQL

前端 未结 1 973
无人共我
无人共我 2021-01-18 08:06

I\'ve been trying to look up for awhile now if it\'s possible to use an alias stated earlier in the select statement if it can be used in a case later in the case statement

1条回答
  •  天涯浪人
    2021-01-18 08:33

    No, you can't refer to the alias elsewhere in the same level of select, other than in the order by clause, because of when Oracle assigns it internally.

    From the documentation (emphasis added):

    You can use a column alias, c_alias, to label the immediately preceding expression in the select list so that the column is displayed with a new heading. The alias effectively renames the select list item for the duration of the query. The alias can be used in the ORDER BY clause, but not other clauses in the query.

    You would need to use an inner query, something like:

    select "Id",
        case "Id"
            when 3
            then 'foo'
            else 'bar'
        end AS "Results"
    from (
        select TABLEA.SomeIDNumber AS "Id",
        from TABLEA
    );
    

    0 讨论(0)
提交回复
热议问题