Tuesday, 19 January 2016

Query to get the wip quantity from backend

Query to get the wip quantity for an item from backend

SELECT SUM(A.B)
              FROM
                (SELECT sum(nvl(wdj.start_quantity,0) - (nvl(wdj.quantity_scrapped,0) + nvl(wdj.quantity_completed, 0)))  B
                FROM MTL_SUPPLY ms ,
                  PO_DISTRIBUTIONS_ALL pod,
                  WIP_ENTITIES WE,
                  wip_discrete_jobs wdj
                WHERE ms.po_header_id     = pod.po_header_id
                AND ms.po_line_id         = pod.po_line_id
                AND pod.WIP_ENTITY_ID     = WE.WIP_ENTITY_ID
                AND ms.po_distribution_id = pod.po_distribution_id
                AND item_id               = 787036
                AND we.wip_entity_id = wdj.wip_entity_id
                AND wdj.status_type = 3 -- Released
                UNION ALL
                SELECT SUM(DECODE (s.supply_type_code, 'SHIPMENT', s.to_org_primary_quantity, s.to_org_primary_quantity )) B
                FROM mtl_system_items i,
                  mtl_parameters p,
                  bom_calendar_dates c,
                  mtl_supply s,
                  mfg_lookups ml,
                  (SELECT DECODE (ms.po_header_id, NULL, DECODE (ms.supply_type_code, 'REQ', DECODE ( ms.from_organization_id, NULL, 18, 20), 12 ), DECODE (ms.supply_type_code, 'SHIPMENT', 35, 'RECEIVING', 36, 1) ) supply_demand_source_type,
                    poh.segment1 Identifier,
                    supply_source_id
                  FROM mtl_supply ms,
                    po_headers_all poh
                  WHERE 1              =1
                  AND poh.po_header_id = ms.po_header_id
                  ) sx
                WHERE 1                     = 1
                AND s.supply_source_id      = sx.supply_source_id
                AND ml.lookup_type          = 'MRP_SUPPLY_DEMAND_SOURCE_TYPE'
                AND ml.lookup_code          = sx.supply_demand_source_type
                AND ( ( s.req_header_id    IS NULL
                AND s.po_header_id         IS NULL )
                OR ( s.req_header_id        = s.req_header_id
                AND s.from_organization_id IS NOT NULL )
                OR ( s.supply_type_code     = 'REQ'
                AND s.from_organization_id IS NULL )
                OR s.po_header_id           = s.po_header_id )
                AND s.to_organization_id    = 4
                AND s.item_id               = 787036
                AND s.destination_type_code = 'INVENTORY'
                AND ( s.to_subinventory    IS NULL
                OR EXISTS
                  (SELECT 'X'
                  FROM mtl_secondary_inventories s2
                  WHERE s2.organization_id  = s.to_organization_id
                  AND s.to_subinventory     = s2.secondary_inventory_name
                  AND s2.inventory_atp_code = 1
                  AND s2.availability_type  = s2.availability_type
                  ) )
                AND i.organization_id           = s.to_organization_id
                AND i.inventory_item_id         = s.item_id
                AND p.organization_id           = s.to_organization_id
                AND p.calendar_code             = c.calendar_code
                AND p.calendar_exception_set_id = c.exception_set_id
                AND NOT EXISTS
                  (SELECT 'X'
                  FROM oe_drop_ship_sources odss
                  WHERE DECODE (s.po_header_id, NULL, s.req_line_id, s.po_line_location_id ) = DECODE (s.po_header_id, NULL, odss.requisition_line_id, odss.line_location_id )
                  )
                AND c.calendar_date = TRUNC (s.expected_delivery_date)
                ) A;

No comments:

Post a Comment