0

我正在尝试在 ORACLE DB 中运行查询,但由于以下错误“I.ID 无效标识符”一直失败。我想要做的是使用过滤器从嵌套查询中选择给定的结果,该过滤器应该使嵌套查询和上级查询相互关联。

The query is:
SELECT i.name, WOW.BI,WOW.BC, WOW.CP

from inv_investments i,
inner join (select 
bi.COSTI BI, 
bc.COSTI BC,
cp.COSTI AS CP

FROM 
(select atom.COSTI from 
(select odf.IF_CST_TOT COSTI
from prj_baselines bl
inner join odf_ca_baseline odf on odf.id = bl.id
WHERE i.id = bl.project_id
ORDER BY bl.created_date DESC) atom where rownum =1)  cp,

(select odf.IF_CST_TOT COSTI
from prj_baselines bl
inner join odf_ca_baseline odf on odf.id = bl.id
where odf.if_budget = 1 
AND i.id = bl.project_id) bi,

(select odf.IF_CST_TOT COSTI
from prj_baselines bl
inner join odf_ca_baseline odf on odf.id = bl.id
Where  if_budget_corrente = 1 
AND i.id = bl.project_id) bc  ) WOW on wow.ID = i.ID

你知道如何解决吗?每次我检查嵌套查询的 where 子句(例如 WHERE i.id = bl.project_id)时,我都会收到错误消息...

4

2 回答 2

3

以下语法不正确:

SELECT i.name, WOW.BI,WOW.BC, WOW.CP
from inv_investments i,
inner join (select . . .

后面有一个逗号i和一个inner join语句。

您的查询有点难以理解,但我认为简单的解决方法是将子句删除并将子句inner join更改为子句。onwhere

于 2013-04-15T14:15:24.047 回答
0

假设每个子查询 cp、bi 和 bc 旨在为每个项目/投资 id 返回一行,您应该能够将单独的子查询组合成一个查询 - 如下所示:

with cte as
(select i.id,
        i.name,
        row_number() over (partition by i.id ORDER BY bl.created_date DESC) rn,
        o.IF_CST_TOT,
        o.if_budget,
        if_budget_corrente
 from inv_investments i
 join prj_baselines b on i.id = b.project_id
 join odf_ca_baseline o on b.id = o.id)
select max(name) name,
       max(case rn when 1 then IF_CST_TOT end) BI,
       max(case if_budget when 1 then IF_CST_TOT end) BC,
       max(case if_budget_corrente when 1 then IF_CST_TOT end) CP
from cte
group by id
于 2013-04-15T14:46:54.477 回答