-- LOT HOLD Records Query
-- Retrieves HOLD/RELEASE history for a LOT
--
-- Parameters:
--   container_id - CONTAINERID to query (16-char hex)
--
-- Note: Uses HOLDTXNDATE/RELEASETXNDATE (NOT TXNDATETIME)
--       NULL RELEASETXNDATE means currently HOLD

SELECT
    CONTAINERID,
    WORKCENTERNAME,
    HOLDTXNDATE,
    HOLDEMP,
    HOLDEMPDEPTNAME,
    HOLDREASONNAME,
    HOLDCOMMENTS,
    RELEASETXNDATE,
    RELEASEEMP,
    RELEASECOMMENTS,
    NCRID,
    CASE
        WHEN RELEASETXNDATE IS NULL THEN 'HOLD'
        ELSE 'RELEASED'
    END AS HOLD_STATUS,
    CASE
        WHEN RELEASETXNDATE IS NULL THEN
            ROUND((SYSDATE - HOLDTXNDATE) * 24, 2)
        ELSE
            ROUND((RELEASETXNDATE - HOLDTXNDATE) * 24, 2)
    END AS HOLD_HOURS
FROM DWH.DW_MES_HOLDRELEASEHISTORY
WHERE CONTAINERID = :container_id
ORDER BY HOLDTXNDATE DESC
