Question

I am trying to run a query within a ORACLE DB but I keep on failing whit the following error "I.ID invalid identifier". What I am trying to do is selecting a given result from a nested query using a filter that is supposed to interrelate the nested query and up-level query.

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

Do you know how to fix it? Each time I check in where clause of a nested query (e.g. WHERE i.id = bl.project_id) i get the error...

Was it helpful?

Solution 2

Assuming that each of the subqueries cp, bi and bc is intended to return one row per project/investment id, you should be able to combine the separate sub-queries into a single query - like so:

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

OTHER TIPS

The following syntax is incorrect:

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

You have a comma after i as well as an inner join statement.

Your query is a bit difficult to follow, but I think the easy fix would be to remove the inner join and change the on clause to a where clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top