Oracle nested select

I have the below SQL for Oracle.

I have added a select with
-- NEW CODE FROM HERE nad -- NEW CODE TO HERE

But I get an error: ORA-00904: "Z"."PART_NO": invalid identifier
on line marked with -- ERROR HERE

Why can't I reference to Z ?
select
z.part_no,
z.description,
--z.JF_onhand,
--z.JFP_Onhand,
z.maxlager,
--z.JFP_Demand54_Today as Demand54,
z.HentFraJ as Hent_Fra_J,
z.HentFraNotJ as Hent_Fra_Ikke_J,
(select ipl.location_no
from KK.INVENTORY_PART_DEF_LOC ipl
where ipl.contract = 'JF' and
      ipl.part_no = z.part_no and 
      ipl.location_type = 'Picking') as TilLokation,
(select
wm_concat(ips.location_no||'='||(ips.qty_onhand - ips.qty_reserved)||'  ')
from
KK.INVENTORY_PART_IN_STOCK ips 
where ips.contract = 'JFP' and ips.part_no = z.part_no and (ips.qty_onhand - ips.qty_reserved) > 0 and substr(ips.location_no,1,1) in ('G','H','J','S','C','D','M') 
) as Lokationer,
'_______________' as HENTET,

-- NEW CODE FROM HERE
(select
--i2.location_no
i2.part_no
from 
(select
i1.contract,
i1.part_no,
i1.location_no,
i1.qty_onhand - i1.qty_reserved as freeqty,
case substr(i1.location_no,1,1)        
 when 'J' then '1'
 when 'M' then '2'
 when 'S' then '3'
 when 'G' then '4'   
 else '9'  
end as sortering
from KK.INVENTORY_PART_IN_STOCK i1 
        where i1.contract = 'JFP' and 
              i1.part_no = z.part_no and -- ERROR HERE
              substr(i1.location_no,1,1) in ('J','G','H','S','C','D','M') and
              (i1.qty_onhand - i1.qty_reserved) > 0
order by i1.part_no, sortering) i2
where rownum = 1) as fralokation 
-- NEW CODE TO HERE


from

(select
y.part_no,
y.description,
--y.location_no,
y.JF_onhand,
y.JFP_Onhand,
y.JFP_Onhand_J,
y.JFP_Onhand_Not_J,
y.MaxLager,
y.JF_Demand_Today,
y.JFP_Demand_Today,
y.JFP_Demand54_Today,
y.DerErPladsTil,
y.Hent,
least(y.JFP_Onhand_j,Hent) as HentFraJ,
least(y.JFP_Onhand_Not_J,Hent - least(y.JFP_Onhand_j,Hent) ) as HentFraNotJ

from

(select
x.contract,
x.part_no,
x.description,
x.location_no,
x.JF_onhand,
x.JFP_Onhand,
x.JFP_Onhand_J,
x.JFP_Onhand_Not_J,
x.MaxLager,
x.JF_Demand_Today,
x.JFP_Demand_Today,
x.JFP_Demand54_Today,

greatest(0,(x.MaxLager - x.JF_onhand)) as DerErPladsTil,
least (x.JFP_Demand54_Today,greatest(0,(x.MaxLager - x.JF_onhand))) as Hent

from

(select
ip.contract,
ip.part_no,
ip.description,
ipl.location_no,
greatest (0,nvl((select sum(ips.qty_onhand) 
        from KK.INVENTORY_PART_IN_STOCK ips 
        where ips.contract = 'JF' and ips.part_no = ip.part_no and ips.location_no = ipl.location_no),0)) as JF_Onhand,

greatest (0,nvl((select sum(ips.qty_onhand - ips.qty_reserved) 
        from KK.INVENTORY_PART_IN_STOCK ips 
        where ips.contract = 'JFP' and ips.part_no = ip.part_no),0)) as JFP_Onhand,
greatest (0,nvl((select sum(ips.qty_onhand - ips.qty_reserved)
        from KK.INVENTORY_PART_IN_STOCK ips 
        where ips.contract = 'JFP' and ips.part_no = ip.part_no and substr(ips.location_no,1,1) = 'J'),0)) as JFP_Onhand_J,
greatest (0,nvl((select sum(ips.qty_onhand - ips.qty_reserved) 
        from KK.INVENTORY_PART_IN_STOCK ips 
        where ips.contract = 'JFP' and ips.part_no = ip.part_no and substr(ips.location_no,1,1) in ('G','H','S','C','D','M')),0)) as JFP_Onhand_Not_J,
        
nvl((select kpc.attr_value_numeric 
        from KK.INVENTORY_PART_CHAR_ALL kpc 
        where kpc.contract = 'JF' and kpc.part_no = ip.part_no and kpc.characteristic_code = 'RES-M'),0) as MaxLager,
        
nvl((select sum(osd.qty_demand)
        from KK.ORDER_SUPPLY_DEMAND_EXT osd        
        where osd.contract = 'JF' and osd.part_no = ip.part_no
        and osd.date_required <= sysdate
        ),0) as JF_Demand_Today,

nvl((select sum(osd.qty_demand)
        from KK.ORDER_SUPPLY_DEMAND_EXT osd        
        where osd.contract = 'JFP' and osd.part_no = ip.part_no and osd.order_no not in ('150','152')
        and osd.date_required <= sysdate  
        ),0) as JFP_Demand_Today,

nvl((select sum(osd.qty_demand)
        from KK.ORDER_SUPPLY_DEMAND_EXT osd        
        where osd.contract = 'JFP' and osd.part_no = ip.part_no and osd.order_no in ('150','152')
        and osd.date_required <= sysdate  
        ),0) as JFP_Demand54_Today

from kk.inventory_part ip
left join KK.INVENTORY_PART_DEF_LOC ipl
     on ipl.contract = ip.contract and
        ipl.part_no = ip.part_no and 
        ipl.location_type = 'Picking'
where ip.contract = 'JF'
and ipl.location_no not in ('P','R') and substr(ipl.location_no,1,1) in ('P','R')) x
where x.MaxLager > 0
) y
where (least(y.JFP_Onhand_j,Hent) > 0 or least(y.JFP_Onhand_Not_J,Hent - least(y.JFP_Onhand_j,Hent) ) > 0))z
where rownum <= 100

Open in new window

VinumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
You have two nested selects and are referencing 'z' in the inner.  You can only reference one level deep.

This simplified example shows what you are trying to do:
select z.dummy, (select dummy from ( select z.dummy from dual x) ) from dual z;
0
VinumAuthor Commented:
Okay - i was afraid of that.

I had to use two nested select because I only needed the top 1 location, after sorting...

(not the Part_No, but Location_No...

(select
i2.location_no
--i2.part_no
from 
(select
i1.contract,
i1.part_no,
i1.location_no,
i1.qty_onhand - i1.qty_reserved as freeqty,
case substr(i1.location_no,1,1)        
 when 'J' then '1'
 when 'M' then '2'
 when 'S' then '3'
 when 'G' then '4'   
 else '9'  
end as sortering
from KK.INVENTORY_PART_IN_STOCK i1 
        where i1.contract = 'JFP' and 
              i1.part_no = z.part_no and 
              substr(i1.location_no,1,1) in ('J','G','H','S','C','D','M') and
              (i1.qty_onhand - i1.qty_reserved) > 0
order by i1.part_no, sortering) i2
where rownum = 1) as fralokation 

Open in new window

0
slightwv (䄆 Netminder) Commented:
>>I only needed the top 1 location

If you are only selecting one value from the nested select, can you maybe use the Data Warehouse Window functions?

If you can provide some simplified sample data and expected results (and your nested select that provides the desired result), I can see if I can remove the nested select.
0
VinumAuthor Commented:
It will be a little too difficult to produce test data.

I will try to find a solution
0
slightwv (䄆 Netminder) Commented:
You cannot just dummy something up that is close to your requirement?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.