Select * from (
SELECT distinct
   CS.Company
  ,S.Contract Site
  ,S.Part_No
  ,i.Description
  ,i.Part_Status Status
  ,TO_CHAR(S.Unit_Cost,'99,990.0000000') Unit_Cost
  ,i.UNIT_MEAS "UM"
  ,C.Currency_Code Curr
  ,TO_CHAR(S.QTY_ONHAND,'999,999,990.00') "Qty OnHand"
  ,TO_CHAR(S.QTY_ONHAND*S.UNIT_COST,'99,999,999,990.00') "Qty OnHand$"
  ,d.Incoming_Dispatch "Incoming Dispatch Qty"
  ,TO_CHAR(CO.Qty_to_Deliver,'9,999,990') "Open CO Qty"
  ,TO_CHAR(CO.Qty_to_Deliver*S.UNIT_COST,'9,999,990.00') "Open CO (Cost)"
  ,TO_CHAR(ipp.Safety_Stock,'99,999,990') "Safety Stock"
  ,TO_CHAR(ipp.Safety_Stock*S.UNIT_COST,'99,999,999,990.00') "Safety Stock$"
  ,TO_CHAR(TP.TPSS,'99,999,990') "TPSS"
  ,TO_CHAR(TP.TPSS*S.UNIT_COST,'99,999,999,990.00') "TPSS$"
  ,TO_CHAR(S.ACI_QTY,'99,999,999,990.00') "ACI Qty"
  ,TO_CHAR(S.QTY_RESERVED,'99,999,999,990.00') "Qty Reserved"
  ,TO_CHAR(S.Available_Qty,'99,999,999,990.00') "Available Qty"
  ,TO_CHAR(HU.HU_Capacity,'999,999') "Qty per HU"  
  ,TO_CHAR(S.QTY_ONHAND/HU.HU_Capacity,'999,999,990.00') "Pallet count"
  ,i.ACCOUNTING_GROUP
  ,i.PART_PRODUCT_FAMILY
  ,Inventory_Product_Family_API.Get_Description(i.PART_PRODUCT_FAMILY) "Product Family"
  ,i.PART_PRODUCT_CODE
  ,Inventory_Product_Code_API.Get_Description(i.PART_PRODUCT_CODE) "Product Code"
  ,C150.Commodity
  ,i.TYPE_DESIGNATION
  ,C28.Color || ' (' || C79.Pantone || ') '  "Pantone color"
  ,C13.Mold_Ref || '-' || C149.Assy_Ref "Mold and Assy 2D_Ref" 
  ,C159.PS_Config || ' ' || C158.Orientation "Sketch config/orientation"
  ,C34.Deco Decoration
  ,W.Where_Used
  ,TO_CHAR(ipp.Min_ORDER_QTY,'99,999,999') Min_Lot_Size
  ,TO_CHAR(ipp.Max_ORDER_QTY,'99,999,999') Max_Lot_Size
  ,TO_CHAR(ipp.MUL_ORDER_QTY,'99,999,999') Mult_Lot_Size
  ,C32.Customer_No "Customer"
  ,CI.Name "Customer Name"
  ,i.Planner_Buyer Planner
  ,p.BUYER_CODE Buyer
  ,p.CF$_PURCHASE_PART_CAT_1 PPC1
  ,p.CF$_PURCHASE_CAT1_DESC PURCHASE_CAT1
  ,p.CF$_PURCHASE_PART_CAT_2 PPC2
  ,p.CF$_PURCHASE_CAT2_DESC PURCHASE_CAT2
  ,p.CF$_PURCHASE_PART_CAT_3 PPC3
  ,p.CF$_PURCHASE_CAT3_DESC PURCHASE_CAT3
  ,i.C_Resin_Type Resin
  ,TO_CHAR(i.C_RESIN_WEIGHT,'9,990.99990') "Part Weight"
  ,i.CF$_SOURCING_OPTION "Sourcing Option"
  ,i.note_text "Note Text"
  ,NI.DocText "Inventory Note"
  ,i.SUPERSEDES
  ,TO_CHAR(i.CREATE_DATE,'yyyy-mm-dd') "Creation date"
  ,TO_CHAR(S.LAST_ACTIVITY_DATE_Min,'yyyy-mm-dd hh:mi') "Last Activity date min"
  ,TO_CHAR(S.LAST_ACTIVITY_DATE_Max,'yyyy-mm-dd hh:mi') "Last Activity date max"
  ,TO_CHAR(S.RECEIPT_DATE_Min,'yyyy-mm-dd hh:mi') "Receipt date min"
  ,TO_CHAR(S.RECEIPT_DATE_Max,'yyyy-mm-dd hh:mi') "Receipt date max"
  ,TO_CHAR(AGE.Days_Old,'9,990') "Days Old"
  ,case when AGE.Days_Old <90 then '[0M-3M]'
		when AGE.Days_Old >=90 and AGE.Days_Old <180 then '[3M-6M]'
		when AGE.Days_Old >=180 and AGE.Days_Old <270 then '[6M-9M]'
		when AGE.Days_Old >=270 and AGE.Days_Old <360 then '[9M-12M]'
		when AGE.Days_Old >=360 and AGE.Days_Old <450 then '[12M-15M]'
		when AGE.Days_Old >=450 and AGE.Days_Old <540 then '[15M-18M]'	
		when AGE.Days_Old >=540 and AGE.Days_Old <630 then '[18M-21M]'
		when AGE.Days_Old >=630 and AGE.Days_Old <720 then '[21M-24M]'		
		when AGE.Days_Old >=720 then '[+24M]'
	end "AGE CLASS"
  ,sysdate as TimeStamp

FROM 
			(Select S.Contract, S.Part_No,S.UNIT_COST
				,sum(S.QTY_ONHAND) QTY_ONHAND
				,sum(S.QTY_RESERVED) QTY_RESERVED
				,sum(S.QTY_IN_TRANSIT) QTY_IN_TRANSIT
				,sum(S.QTY_ONHAND - S.QTY_RESERVED) Available_Qty
				,SUM(CASE WHEN s.AVAILABILITY_CONTROL_ID is not null THEN S.QTY_ONHAND ELSE 0 END) ACI_QTY
				,min(S.LAST_ACTIVITY_DATE) LAST_ACTIVITY_DATE_Min
				,max(S.LAST_ACTIVITY_DATE) LAST_ACTIVITY_DATE_Max
				,min(S.RECEIPT_DATE) RECEIPT_DATE_Min
				,max(S.RECEIPT_DATE) RECEIPT_DATE_Max
				From Inventory_part_in_stock_UIV S
				Where S.QTY_ONHAND>0 
				Group by S.Contract, S.Part_No,S.UNIT_COST) S
				
			inner join COMPANY_SITE CS on S.Contract=CS.Contract
			
			inner join Company_Finance C on C.Company=CS.Company
			
			inner join inventory_part_cfv	i	on S.Contract = i.Contract      and S.Part_No = i.Part_No
			
			
			left join (Select contract,Part_no,BUYER_CODE,CF$_PURCHASE_PART_CAT_1,CF$_PURCHASE_PART_CAT_2,CF$_PURCHASE_PART_CAT_3,CF$_PURCHASE_CAT1_DESC,CF$_PURCHASE_CAT2_DESC,CF$_PURCHASE_CAT3_DESC
			from purchase_part_cfv) p on p.contract=i.contract and p.part_no=i.part_no
			
			
            inner join Inventory_Part_Planning	ipp on S.Contract = ipp.Contract  and S.Part_No = ipp.Part_No
			
			left join  (Select Part_No,max(max_quantity_capacity) HU_Capacity from Part_Handling_unit where handling_unit_type_id in ('STACK','PALLET') group by Part_No) HU on HU.Part_No = S.Part_No
			
			left join (Select dt.Note_Text DocText,i.Part_No from document_text dt inner join inventory_part i on i.Note_ID=dt.Note_ID where dt.output_type='INVENTORY' ) NI on NI.Part_No=S.Part_No
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Customer_No from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE='00032') C32 
     on S.Part_No=C32.PART_NO and S.Contract=C32.Contract
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Deco from INVENTORY_PART_CHAR_ALL where (CHARACTERISTIC_CODE='00034' and INVENTORY_PART_API.Get_Eng_Attribute(contract,PART_NO) in ('00008','00010')) or (CHARACTERISTIC_CODE='00063' and INVENTORY_PART_API.Get_Eng_Attribute(contract,PART_NO) ='00006')   ) C34 
     on S.Part_No=C34.PART_NO and S.Contract=C34.Contract 
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Pantone from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00079')) C79 
     on S.Part_No=C79.PART_NO and S.Contract=C79.Contract 	
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Color from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00028')) C28 
     on S.Part_No=C28.PART_NO and S.Contract=C28.Contract
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Mold_Ref from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00013')) C13 
     on S.Part_No=C13.PART_NO and S.Contract=C13.Contract 	
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Assy_Ref from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00149')) C149 
     on S.Part_No=C149.PART_NO and S.Contract=C149.Contract
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Commodity from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00150')) C150 
     on S.Part_No=C150.PART_NO and S.Contract=C150.Contract
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA Orientation from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00158')) C158 
     on S.Part_No=C158.PART_NO and S.Contract=C158.Contract 	
			left join (select PART_NO, CONTRACT, ATTR_VALUE_ALPHA PS_Config from INVENTORY_PART_CHAR_ALL where CHARACTERISTIC_CODE in ('00159')) C159 
     on S.Part_No=C159.PART_NO and S.Contract=C159.Contract	 	 
			left join CUSTOMER_INFO CI  on C32.Customer_No=CI.Customer_ID
			
								
				
			left join (Select C.Part_No,C.Contract,sum(C.BUY_QTY_DUE - C.QTY_SHIPPED) Qty_to_Deliver From customer_order_join_cfv C Where C.objstate in ('Released','Blocked','Reserved','Picked','PartiallyDelivered') Group By C.Part_No, C.Contract) CO on S.Part_No=CO.Part_No and S.Contract=CO.Contract
			left join (Select U.Contract,U.COMPONENT_PART,count(U.COMPONENT_PART) Where_Used
				From MANUF_STRUCTURE U Where MANUF_STRUCT_ALTERNATE_API.Get_State(U.contract,U.part_no,U.eng_chg_level,U.bom_type,U.alternative_no)='Buildable' and U.EFF_PHASE_OUT_DATE is null and U.Parent_part_Status in ('A','D') and U.BOM_TYPE_DB='M' Group by U.Contract,U.COMPONENT_PART) W on W.Contract=S.Contract and W.Component_Part=S.Part_No
				
			inner join (Select Contract,Part_no, sum((sysdate-RECEIPT_DATE)*QTY_ONHAND) / nullif(sum(QTY_ONHAND),0) Days_Old
From INVENTORY_PART_IN_STOCK_UIV Group by Contract,Part_No) AGE on s.Part_No=AGE.Part_No and S.Contract = AGE.Contract

			left join (Select Part_No, Contract, BUILD_UP_QTY TPSS  from STRATEGIC_SAFE_STOCK_PART where sysdate BETWEEN FROM_DATE and TO_DATE) TP on S.Contract = TP.Contract and S.Part_No=TP.Part_No
			
			left join (Select d.Contract,d.SENDER_PART_NO,sum(d.DISPATCH_SOURCE_QTY) Incoming_Dispatch From RECEIVABLE_DISPATCH_ADV_LINE d Where d.State='Created' Group by d.Contract, d.Sender_Part_No) d on S.Contract=d.Contract and S.Part_No=d.SENDER_PART_NO


WHERE i.PART_PRODUCT_FAMILY not in ('F0000','F0001','F0100') and CS.Company like nvl ('&Company' , '%' ) and S.CONTRACT LIKE '%&Site%' and S.Part_No like nvl ('&Part_NO','%') and i.PART_PRODUCT_FAMILY like nvl ('&Product_Family','%') and i.ACCOUNTING_GROUP like nvl ('&Accounting_Group','%') and s.part_no not in ('1087188')
) F

where (F.PPC1 like nvl('&PURCHASE_CAT1','%') and F.PPC2 like nvl('&PURCHASE_CAT2','%') and F.PPC3 like nvl('&PURCHASE_CAT3','%'))