0

为什么我不能使用子查询因式分解子句导致 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')
4

1 回答 1

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')
于 2018-04-03T02:27:34.870 回答