Saturday, 18 October 2014

SQL Script -Discrete Inventory org On hand stock availablity with subinventory,locator,lot with cost

This SQL Script give the details of On hand stock for Discrete Inventory organization.
Report Output have Item wise, Category wise, Sub Inventory Wise, Locator wise, Lot wise On hand stock with cost price.

 select org_name,segment1,description,PRIMARY_UNIT_OF_MEASURE,category_concat_segs,SUBINVENTORY_CODE,LOT_NUMBER,locator1,sum(TRANSACTION_QUANTITY),division,Rate,Status
from
(select --rownum sno,--msi.organization_id,
substr(ood.organization_name,5) org_name
,msi.segment1
,msi.description,
--mic.segment1 xx,
--mic.segment2,
-- mic.segment3,
  --mic.segment4,
   --msi.inventory_item_flag
   msi.PRIMARY_UNIT_OF_MEASURE,
   mic.category_concat_segs,
moq.SUBINVENTORY_CODE,
moq.LOT_NUMBER,
(select SEGMENT1||SEGMENT2 LOCATOR from MTL_ITEM_LOCATIONS where INVENTORY_LOCATION_ID=Moq.locator_id)locator1,
moq.TRANSACTION_QUANTITY --,moq.LOCATOR_ID
,
(select distinct kkk.description
from FND_FLEX_VALUES_VL kkk,MTL_SUBINVENTORIES_ALL_V lll
where 1=1
--and DESCRIPTION like 'Corporate%'
and kkk.FLEX_VALUE_SET_ID=1014875
and kkk.FLEX_VALUE=lll.attribute1
and lll.organization_id=nvl(:P_ORG,lll.organization_id)
and lll.SECONDARY_INVENTORY_NAME =moq.SUBINVENTORY_CODE) division,
(select A.FULL_NAME||A.EMPLOYEE_NUMBER  name
from per_all_people_f A,MTL_SUBINVENTORIES_ALL_V B where 1=1 and A.PERSON_ID=B.ATTRIBUTE10
and  b.SECONDARY_INVENTORY_NAME=moq.SUBINVENTORY_CODE
and b.organization_id=nvl(:P_ORG,b.organization_id)) EMP_NAME,
(SELECT   ROUND (item_cost, 2) item_cost
  FROM   cst_item_costs
WHERE       inventory_item_id = msi.INVENTORY_ITEM_ID
         AND organization_id =msi.ORGANIZATION_ID
         AND cost_type_id = 2) RATE,
CASE
   WHEN msi.inventory_item_flag='Y' THEN 'INVENTORY'
   WHEN msi.inventory_item_flag='N' THEN 'NON INVENTORY'
   ELSE 'NU'
END   STATUS
from mtl_system_items_b msi,mtl_item_categories_v mic,ORG_ORGANIZATION_DEFINITIONS ood,MTL_ONHAND_QUANTITIES MOQ
where mic.organization_id=msi.organization_id
and mic.inventory_item_id=msi.inventory_item_id
and   mic.organization_id=ood.organization_id
and mOQ.organization_id(+)=msi.organization_id
and MOQ.inventory_item_id(+)=msi.inventory_item_id
and upper(MIC.CATEGORY_SET_NAME)='INVENTORY'
--and moq.SUBINVENTORY_CODE like 'CUST_SUP%'
--and msi.organization_id not in (95,96)
and msi.organization_id=nvl(:P_ORG, msi.organization_id)
and nvl(moq.TRANSACTION_QUANTITY,0)<>0
order by  mic.segment1)
group by org_name,segment1,description,PRIMARY_UNIT_OF_MEASURE,category_concat_segs,SUBINVENTORY_CODE,LOT_NUMBER,locator1,division,Rate,Status