如何在以下查询中访问别名

时间:2014-07-22 18:57:34

标签: sql oracle decode

下面是我的查询,我想访问UNQKEY值,我可以按顺序使用它,但我想在where条件中使用它。有没有想要使用UNQKEY作为条件的一部分?

SELECT distinct TOCID,'UNQKEY' || r UNQKEY  ,TOCNAME,
  decode( r, 1, UNQKEY1, 2, UNQKEY2, 3, UNQKEY3, 4, UNQKEY4 ,   5, UNQKEY5) AS 
  UNQKEY_VALUE
FROM TOC, RULE RL,
  (select rownum r
    from all_objects
    where rownum <=5) 
WHERE TOCID=1210 
ORDER BY UNQKEY

1 个答案:

答案 0 :(得分:0)

如果要在where子句中使用它,请使用子查询或CTE:

select *
from (select distinct TOCID, 'UNQKEY' || r as UNQKEY , TOCNAME,
             decode( r, 1, UNQKEY1, 2, UNQKEY2, 3, UNQKEY3, 4, UNQKEY4 ,   5, UNQKEY5) AS UNQKEY_VALUE
      from TOC, RULE RL,
           (select rownum r
            from all_objects
            where rownum <=5
           )
      ) t
WHERE TOCID = 1210 and UNQKEY like '%%'
order by UNQKEY;