在 where 子句中使用子查询分解结果

using subquery factoring result in where clause

为什么我不能在 where 子句中使用子查询分解子句,如下所示 sql:

with rpt as(
 select * from reports where caseid = 
 :case_id and rownum=1 order by created desc
)
select 
 distinct rt.trialid
from 
 report_trials rt
join 
 trial_genes tg on rt.id=tg.trialid
where 
 rt.reportid = rpt.id
and 
tg.gene not in('TMB','MS')

子查询命名为 rpt 并用在 select 语句的 where 子句中。执行时遇到如下错误:ORA-00904: "RPT"."ID": invalid identifier

更新

事实上,针对同一事物的嵌套查询也给了我同样的问题。嵌套子查询仅从单行返回单列值:

select 
 distinct rt.trialid
from 
  report_trials rt
  join 
  trial_genes tg on rt.id=tg.trialid
where 
 rt.reportid = (select id from reports where caseid = :case_id and 
  rownum=1 order by created desc)
and 
 tg.gene not in('TMB','MS')

您没有在查询中添加 table rpt,因此出现错误。

with rpt as(
 select * from reports where caseid = 
 :case_id and rownum=1 order by created desc
)
select 
 distinct rt.trialid
from 
 report_trials rt
join 
 trial_genes tg on rt.id=tg.trialid
join 
  rpt on rt.reportid = rpt.id
where  
  tg.gene not in('TMB','MS')