Tuesday, November 5, 2013

Identifying Duplicate Shipments SQL Query

 SELECT rsl.shipment_header_id,
        rsl.shipment_line_id,
        rsh.shipment_num
 FROM   rcv_shipment_lines rsl,
        rcv_shipment_headers rsh
 WHERE  rsl.shipment_header_id = rsh.shipment_header_id
 AND    rsh.creation_date  >= To_Date('&from_date', 'DD-MON-YYYY HH24:MI:SS')
 AND    rsh.creation_date  <= To_Date('&to_date', 'DD-MON-YYYY HH24:MI:SS')
 AND    rsl.mmt_transaction_id IS NOT null
 AND    rsh.receipt_source_code = 'INVENTORY'
 AND EXISTS
    (
     SELECT 1
     FROM mtl_parameters mp
     WHERE mp.organization_id = rsh.organization_id
    )
 AND NOT EXISTS
    (
     SELECT 1
     FROM mtl_material_transactions mmt
     WHERE mmt.transaction_id = rsl.mmt_transaction_id
     AND   mmt.transaction_source_type_id = 13
     AND   mmt.transaction_action_id in (3,21)
    )
 AND NOT EXISTS
    (
     SELECT 1
     FROM  mtl_material_transactions mmt,
           rcv_transactions rt
     WHERE mmt.rcv_transaction_id = rt.transaction_id
     AND   rt.shipment_line_id = rsl.shipment_line_id
     AND   rt.transaction_type = 'DELIVER'
    )
UNION
 SELECT rsl.shipment_header_id,
        rsl.shipment_line_id,
        rsh.shipment_num
 FROM   rcv_shipment_lines rsl,
        rcv_shipment_headers rsh
 WHERE  rsl.shipment_header_id = rsh.shipment_header_id
 AND    rsh.creation_date  >= To_Date('&from_date', 'DD-MON-YYYY HH24:MI:SS')
 AND    rsh.creation_date  <= To_Date('&to_date', 'DD-MON-YYYY HH24:MI:SS')
 AND    rsl.mmt_transaction_id IS NOT null
 AND    rsh.receipt_source_code = 'INTERNAL ORDER'
 AND EXISTS
    (
     SELECT 1
     FROM mtl_parameters mp
     WHERE mp.organization_id = rsh.organization_id
    )
 AND NOT EXISTS
    (
     SELECT 1
     FROM mtl_material_transactions mmt
     WHERE mmt.transaction_id = rsl.mmt_transaction_id
     AND   mmt.transaction_source_type_id = 8
     AND   mmt.transaction_action_id in (1,3,21,2)
    )
 AND NOT EXISTS
    (
     SELECT 1
     FROM  mtl_material_transactions mmt,
           rcv_transactions rt
     WHERE mmt.rcv_transaction_id = rt.transaction_id
     AND   rt.shipment_line_id = rsl.shipment_line_id
     AND   rt.transaction_type = 'DELIVER'
    )
 ORDER BY 1;

No comments: