使用子查询因子结果在where子句中

时间:2018-04-03 01:49:46

标签: oracle subquery-factoring

为什么我不能在以下sql中描述的where子句中使用子查询因子子句结果:

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')

1 个答案:

答案 0 :(得分:0)

您错过了在查询中添加表格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')