单独运行以下sql查询时,将得到三个字母值的正确结果
(select substr((select code from (select qq.id, qq.code from contacts qq where qq.code like 'FB%' and qq.ref_no = 3359245 order by id desc ) where rownum=1),3,6) from dual)但当我以以下方式运行相同的查询时,
select ce.ref_no,
(select substr((select code from (select qq.id, qq.code from contacts qq where qq.code like 'FB%' and qq.ref_no = ce.ref_no order by id desc ) where rownum=1),3,6) from dual) "FEEDBACK"
from contacts ce
where ce.ref_no = 3359245;我收到以下错误
ORA-00904: "CE"."REF_NO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 62 Column: 137我的数据如下:
14494895 FBBOM
14494896 FBDEL
14494897 FBBOM
14494898 FBDEL
14494902 FBDEL
14494903 FBDEL我想从14494903得到FBDEL
发布于 2014-06-20 14:36:18
在使用关联子查询时,Oracle对它识别外部查询的“深度”有一个限制。只能下降一层。
您有两个级别的关联条件,因此编译器不识别外部别名。
我认为你可以用解析函数更容易地做你想做的事。我想这就是你想要的:
select ce.ref_no,
substr(max(code) keep (dense_rank first order by (case when code like 'FB%' then 1 else 0 end) desc,
id desc
), 3, 6)
from contacts ce
where ce.ref_no = 3359245
group by ce.ref_no;这确实假设存在一个FB记录,因此在其他情况下它可能返回错误的东西。(您可以使用额外的逻辑来检查这一点。)
发布于 2014-06-20 14:29:31
这是因为Contact表和别名ce对内部查询不可见。
https://stackoverflow.com/questions/24329305
复制相似问题