在Oracle SQL中使用案例语句的部分时使用别名

如果可以在后面的Oracle SQL案例语句中使用它可以在select语句中使用前面说明的别名,我一直在尝试查找一段时间.我发现的大多数结果都是关于如何根据不同问题的case语句创建Alias.一个简单的例子是:

Select TABLEA.SomeIDNumber AS "Id",case ID
          when 3
          then 'foo'
          else 'bar'
       end AS "Results"
FROM OMEGA.TABLEA

在我正在创建的SQL语句中真的不那么简单(它实际上是基于以前的case语句创建的,并且需要在各种表上进行一些连接来完成查询的其他完整拍摄,但是如果不知道它就不会有意义更多我无法分享的数据库).

我只是想知道以后在Oracle的select语句中是否可以在case语句中使用别名(我知道可以通过Access类似“SQL”来完成这些事情).或者我是否更好地重新选择select以使其成为嵌套的select语句?可能是可行的,只是更多的痛苦.

解决方法

不,除了在order by子句中之外,你不能在select的同一级别中引用别名,因为Oracle在内部分配它.

From the documentation(重点补充):

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.

您需要使用内部查询,例如:

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

dawei

【声明】:天津站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。