Select CS.COMPANY,CO.CONTRACT, CH.ATTR_VALUE_ALPHA Commodity, DECO.ATTR_VALUE_ALPHA Decoration, CO.CATALOG_NO, CO.CATALOG_DESC, CO.REAL_SHIP_DATE, CO.ORDER_NO,CO.LINE_NO,CO.REL_NO,CO.CUSTOMER_NO,CU.NAME,DECODE(CO.CATALOG_TYPE_DB, 'INV', CO.QTY_SHIPPED, NULL) Qty, CH2.LAST_LEVEL_Commodity, LAST_LEVEL, CH2.Description Last_Level_Description, DECODE(CO.CATALOG_TYPE_DB, 'INV', CO.QTY_SHIPPED, NULL)*Factor LAST_LEVEL_QTY

From CUSTOMER_ORDER_LINE CO 
inner join COMPANY_SITE CS on CS.CONTRACT=CO.CONTRACT and CO.STATE in ('Delivered','Partially Delivered','Invoiced/Closed')
inner join CUSTOMER_INFO CU on CU.CUSTOMER_ID=CO.CUSTOMER_NO
left join (select contract,part_no,ATTR_VALUE_ALPHA from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE='00150') CH on CH.Contract=CO.Contract and CH.PART_NO=CO.CATALOG_NO 
left join (select contract,part_no,ATTR_VALUE_ALPHA from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE='00034') DECO on DECO.Contract=CO.Contract and DECO.PART_NO=CO.CATALOG_NO 



left join 
(
Select distinct 1 LEVEL_NO,MS.Part_No SALES_PART,'' Level_2_PART,0 Level_2_QTY,'' Level_3_Part,0 Level_3_Qty, MS.Part_No LAST_LEVEL, 1 Factor

From  MANUF_STRUCTURE MS 
inner join Inventory_Part IP on MS.Contract=IP.Contract and MS.PART_NO=IP.PART_NO
inner join INVENTORY_PART_PLANNING IPP on IPP.Contract=MS.Contract and IPP.PART_NO=MS.PART_NO
inner join ROUTING_OPERATION RO on RO.Contract=MS.Contract and RO.PART_NO=MS.PART_NO
inner join WORK_CENTER WC on WC.WORK_CENTER_NO=RO.WORK_CENTER_NO

Where MS.BOM_TYPE_DB='M' and MANUF_STRUCT_ALTERNATE_API.Get_State(MS.contract,MS.part_no,MS.eng_chg_level,MS.bom_type,MS.alternative_no)='Buildable' and MS.EFF_PHASE_OUT_DATE is NULL and IP.TYPE_CODE_db='1' and IPP.PLANNING_METHOD not in ('P')
and RO.ALTERNATIVE_NO='*' and Routing_Alternate_API.Get_State(RO.CONTRACT,RO.PART_NO,RO.ROUTING_REVISION,RO.BOM_TYPE,RO.ALTERNATIVE_NO)='Buildable' and RO.BOM_TYPE_DB='M' and RO.OPERATION_NO='10' and WC.DEPARTMENT_NO in ('INJEC','DAIRY','BLOW')


UNION

Select distinct 2 LEVEL_NO,MS.Part_No SALES_PART,MS.COMPONENT_PART Level_2_PART,MS.QTY_PER_ASSEMBLY Level_2_QTY,'' Level_3_Part,0 Level_3_Qty, MS.COMPONENT_PART LAST_LEVEL, MS.QTY_PER_ASSEMBLY Factor

From  MANUF_STRUCTURE MS 
inner join Inventory_Part IP on MS.Contract=IP.Contract and MS.COMPONENT_PART=IP.PART_NO
inner join INVENTORY_PART_PLANNING IPP on IPP.Contract=MS.Contract and IPP.PART_NO=MS.COMPONENT_PART
inner join ROUTING_OPERATION RO on RO.Contract=MS.Contract and RO.PART_NO=MS.COMPONENT_PART
inner join WORK_CENTER WC on WC.WORK_CENTER_NO=RO.WORK_CENTER_NO

Where MS.BOM_TYPE_DB='M' and MANUF_STRUCT_ALTERNATE_API.Get_State(MS.contract,MS.part_no,MS.eng_chg_level,MS.bom_type,MS.alternative_no)='Buildable' and MS.EFF_PHASE_OUT_DATE is NULL and IP.TYPE_CODE_db='1' and IPP.PLANNING_METHOD not in ('P')
and RO.ALTERNATIVE_NO='*' and Routing_Alternate_API.Get_State(RO.CONTRACT,RO.PART_NO,RO.ROUTING_REVISION,RO.BOM_TYPE,RO.ALTERNATIVE_NO)='Buildable' and RO.BOM_TYPE_DB='M' and RO.OPERATION_NO='10' and WC.DEPARTMENT_NO in ('INJEC','DAIRY','BLOW')


UNION

Select distinct 3 LEVEL_NO,MS.Part_No SALES_PART,MS.COMPONENT_PART Level_2_PART,MS.QTY_PER_ASSEMBLY Level_2_QTY,MS2.COMPONENT_PART Level_3_Part,MS2.QTY_PER_ASSEMBLY Level_3_Qty,MS2.COMPONENT_PART LAST_LEVEL, MS.QTY_PER_ASSEMBLY*MS2.QTY_PER_ASSEMBLY Factor

From  MANUF_STRUCTURE MS 
inner join Inventory_Part IP on MS.Contract=IP.Contract and MS.COMPONENT_PART=IP.PART_NO
inner join INVENTORY_PART_PLANNING IPP on IPP.Contract=MS.Contract and IPP.PART_NO=MS.COMPONENT_PART
inner join ROUTING_OPERATION RO on RO.Contract=MS.Contract and RO.PART_NO=MS.COMPONENT_PART
inner join WORK_CENTER WC on WC.WORK_CENTER_NO=RO.WORK_CENTER_NO
inner join MANUF_STRUCTURE MS2 on MS2.Contract=MS.Contract and MS2.PART_NO= MS.Component_Part
inner join Inventory_Part IP2 on MS2.Contract=IP2.Contract and MS2.COMPONENT_PART=IP2.PART_NO
inner join INVENTORY_PART_PLANNING IPP2 on IPP2.Contract=MS2.Contract and IPP2.PART_NO=MS2.COMPONENT_PART
inner join ROUTING_OPERATION RO2 on RO2.Contract=MS2.Contract and RO2.PART_NO=MS2.COMPONENT_PART
inner join WORK_CENTER WC2 on WC2.WORK_CENTER_NO=RO2.WORK_CENTER_NO

Where MS.BOM_TYPE_DB='M' and MANUF_STRUCT_ALTERNATE_API.Get_State(MS.contract,MS.part_no,MS.eng_chg_level,MS.bom_type,MS.alternative_no)='Buildable' and MS.EFF_PHASE_OUT_DATE is NULL and IP.TYPE_CODE_db='1' and IPP.PLANNING_METHOD not in ('P')
and RO.ALTERNATIVE_NO='*' and Routing_Alternate_API.Get_State(RO.CONTRACT,RO.PART_NO,RO.ROUTING_REVISION,RO.BOM_TYPE,RO.ALTERNATIVE_NO)='Buildable' and RO.BOM_TYPE_DB='M' and RO.OPERATION_NO='10' and WC.DEPARTMENT_NO in ('OFFSE','ASSY')
and
MS2.BOM_TYPE_DB='M' and MANUF_STRUCT_ALTERNATE_API.Get_State(MS2.contract,MS2.part_no,MS2.eng_chg_level,MS2.bom_type,MS2.alternative_no)='Buildable' and MS2.EFF_PHASE_OUT_DATE is NULL and IP2.TYPE_CODE_db='1' and IPP2.PLANNING_METHOD not in ('P')
and RO2.ALTERNATIVE_NO='*' and Routing_Alternate_API.Get_State(RO2.CONTRACT,RO2.PART_NO,RO2.ROUTING_REVISION,RO2.BOM_TYPE,RO2.ALTERNATIVE_NO)='Buildable' and RO2.BOM_TYPE_DB='M' and RO2.OPERATION_NO='10' and WC2.DEPARTMENT_NO in ('INJEC','DAIRY','BLOW')
) LVL on CO.CATALOG_NO=LVL.SALES_PART


outer apply (select IPC.contract,IPC.part_no,IPC.ATTR_VALUE_ALPHA LAST_LEVEL_Commodity, IP.Description from INVENTORY_PART_CHAR_ALL IPC inner join Inventory_Part IP on IPC.Contract=IP.Contract and IPC.Part_No=IP.Part_No where IP.PART_NO=LVL.Last_Level and  IPC.CHARACTERISTIC_CODE='00150' and IP.TYPE_CODE_DB='1' and IP.PART_STATUS in ('A','D') and rownum<2) CH2 


Where CS.COMPANY in '&Company' and trunc(CO.REAL_SHIP_DATE) BETWEEN  TO_DATE('&DATE_FROM','YYYY-MM-DD')    AND   TO_DATE ('&DATE_TO','YYYY-MM-DD')
and length(CO.CUSTOMER_NO)>5