-- LOT Reject Records Query
-- Retrieves reject (defect) records for a LOT
--
-- Parameters:
--   :container_id - CONTAINERID to query (16-char hex)
--
-- Note: Uses LOSSREASONNAME (NOT REJECTREASONNAME)
--       Uses TXNDATE (NOT TXNDATETIME)
--       Only has EQUIPMENTNAME, NO EQUIPMENTID field
--       DEFECTQTY = SUM of REJECTQTY + STANDBYQTY + QTYTOPROCESS + INPROCESSQTY + PROCESSEDQTY

SELECT
    CONTAINERID,
    LOSSREASONNAME,
    REJECTQTY,
    NVL(REJECTQTY, 0) + NVL(STANDBYQTY, 0) + NVL(QTYTOPROCESS, 0)
        + NVL(INPROCESSQTY, 0) + NVL(PROCESSEDQTY, 0) AS DEFECTQTY,
    WORKCENTERNAME,
    EQUIPMENTNAME,
    TXNDATE,
    COMMENTS,
    REJECTCAUSE,
    REJECTCOMMENT
FROM DWH.DW_MES_LOTREJECTHISTORY
WHERE CONTAINERID = :container_id
ORDER BY TXNDATE
