Select
OH.Contract Site
,WC.DEPARTMENT_NO Department
,SOO.PREFERRED_RESOURCE_ID Pref_Resource
,SOO.Cf$_MOLD Mold
,OH.Dated
,TO_CHAR(OH.Dated,'yyyy-mm') "Year-Month"
,OH.Part_No
,PC.Description
,OH.ORDER_NO
,OH.Release_No
,OH.Sequence_No
,SO.State
,Inventory_Transaction_Hist_API.Get_Reject_Code(OH.INV_TRANSACTION_ID) "Scrapping Cause"
,Scrapping_Cause_API.Get_reject_Message(Inventory_Transaction_Hist_API.Get_Reject_Code(OH.INV_TRANSACTION_ID)) "Scrapping Cause Description"
,CASE WHEN OH.TRANSACTION_CODE='UNOPFDSCP' then -OH.QTY_SCRAPPED ELSE OH.QTY_SCRAPPED END "QTY SCRAPPED"
,(CASE WHEN OH.TRANSACTION_CODE='UNOPFDSCP' then -OH.QTY_SCRAPPED ELSE OH.QTY_SCRAPPED END)*C.INVENTORY_VALUE "Qty Scrapped$"
,SO.CF$_QTY_SHOP_ORDER+SO.OPERATION_SCRAPPED "Lot Size"
,SO.QTY_COMPLETE "QTY_COMPLETE"
,SO.OPERATION_SCRAPPED "SO Scrap"
,case when SO.QTY_COMPLETE=0 then 1 else SO.OPERATION_SCRAPPED/SO.QTY_COMPLETE end "SO Scrap%"
,SO.OPERATION_SCRAPPED*C.INVENTORY_VALUE "SO Scrap$"
,OH2.LABOR_COST
,SO.ORDER_CODE_DB
,SO.ORDER_CODE
,C.INVENTORY_VALUE "Std Cost"
,SO.CF$_SHOP_ORDER_NOTE2 "Shop Order Note"
,Child.Lot_Batch_No "Child Lot Batch"
,Child.CF$_LOT_BATCH_MOLD "Child mold"
From
OPERATION_HISTORY OH
inner join Part_Catalog PC on OH.Part_No=PC.Part_No
inner join Shop_Ord_cfv SO on SO.Order_No=OH.Order_No and SO.Release_No=OH.Release_No and SO.Sequence_No=OH.Sequence_No and SO.Contract=OH.Contract
inner join SHOP_ORDER_OPERATION_JOIN_cfv SOO on SOO.ORDER_NO=OH.ORDER_NO and SOO.RELEASE_NO=OH.RELEASE_NO and SOO.Sequence_No=OH.Sequence_No and SOO.OPERATION_NO=OH.OPERATION_NO
inner join WORK_CENTER_RESOURCE WCR on WCR.RESOURCE_ID=SOO.PREFERRED_RESOURCE_ID and WCR.Contract=SOO.Contract
inner join WORK_CENTER WC on WC.Contract=WCR.Contract and WC.Work_Center_No=WCR.Work_Center_No
inner join INVENTORY_PART_UNIT_COST_SUM C on C.Part_No=OH.Part_No and C.Contract=OH.Contract

left join (Select ORDER_NO, RELEASE_NO, SEQUENCE_NO, sum(LABOR_COST) Labor_Cost from OPERATION_HISTORY where TRANSACTION_CODE='LABOR_RPT' and OPERATION_NO='10' group by ORDER_NO, RELEASE_NO, SEQUENCE_NO) OH2 on OH2.Order_No=SO.Order_No and OH2.Release_No=SO.Release_No and OH2.Sequence_No=SO.Sequence_No

OUTER apply  (SELECT  MH.TIME_STAMP,MH.LOT_BATCH_NO, MH.CF$_LOT_BATCH_MOLD, MH.order_REF1,MH.order_REF2,MH.order_REF3
FROM MATERIAL_HISTORY_cfv MH WHERE material_history_action_db='ISSUE' AND  MH.order_REF1=SO.Order_No and  MH.order_REF2=SO.Release_No and  MH.order_REF3=SO.Sequence_No AND rownum =1   ORDER BY   MH.TIME_STAMP,MH.order_REF1,MH.order_REF2,MH.order_REF3) Child
Where
trunc(OH.Dated) Between    TO_DATE('&DATE_FROM','YYYY-MM-DD')    AND   TO_DATE ('&DATE_TO','YYYY-MM-DD')
and SO.Contract like nvl('&Site','%') and SO.ORDER_CODE_DB like nvl('&SO_TYPE','%') and OH.QTY_SCRAPPED>0 and SOO.Operation_No='10' 
and (decode('&Department','','TRUE')='TRUE' or WC.DEPARTMENT_NO like NVL ('&Department','%')) 
and (decode('&Preferred_Resource','','TRUE')='TRUE' or SOO.PREFERRED_RESOURCE_ID like nvl ('&Preferred_Resource','%')) 
and (decode('&Mold' ,'', 'TRUE')='TRUE' OR cf$_Mold LIKE  nvl('&Mold','%')) 
and OH.ORDER_NO like nvl ('&Order_No','%')