ORA-00904嵌套选择的无效标识符

时间:2015-02-20 16:53:35

标签: oracle where-clause identifier

我被困在这个错误上2个小时。

我嵌套了select以获得第一个值。

select tbl.table_name,
       (select distinct(FirstItem)
       from
           (select first_value(column_name) over (order by timestamp asc rows unbounded predecing) as FirstItem
           from log_table_b l
           where tbl.assignment_no = l.rpt_no)
       ) as "USERNAME",
from prod_table tbl;

它返回此错误:

ERROR at line 6:
ORA-00904: "TBL"."ASSIGNMENT_NO": invalid identifier

我尝试了很多东西,似乎没有人帮助过我。

2 个答案:

答案 0 :(得分:4)

您不能在内部子查询中使用父表。在这里你可以如何做到这一点:

with tmp_table as 
(
    select rpt_no, first_value(column_name) over (order by timestamp asc rows unbounded predecing) as FirstItem
    from log_table_b l
) select distinct tbl.table_name, firstItem
from prod_table tbl
join tmpTable on tmp_table.rpt_no = tbl.assignment_no;

您可能希望为 tmp_table

找到更具描述性的名称

答案 1 :(得分:0)

问题是您只能将外部查询的引用传递给下一个子查询级别。

以下是两种选择:

select tbl.table_name,
       (select min(column_name) keep (dense_rank first order by tstamp asc)
        from   log_table_b l
        where  tbl.assignment_no = l.rpt_no
       ) as "USERNAME"
from   prod_table tbl;

select tbl.table_name,
       l.username
from   prod_table tbl
       inner join (select rpt_no,
                          min(column_name) keep (dense_rank first order by tstamp asc) username
                   from   log_table_b l
                   group by rpt_no)
         on (tbl.assignment_no = l.rpt_no);

N.B。未测试