-- 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
    h.CONTAINERID,
    NVL(TRIM(c.CONTAINERNAME), TRIM(h.CONTAINERID)) AS CONTAINERNAME,
    h.WORKCENTERNAME,
    h.HOLDTXNDATE,
    h.HOLDEMP,
    h.HOLDEMPDEPTNAME,
    h.HOLDREASONNAME,
    h.HOLDCOMMENTS,
    h.RELEASETXNDATE,
    h.RELEASEEMP,
    h.RELEASECOMMENTS,
    h.NCRID,
    CASE
        WHEN h.RELEASETXNDATE IS NULL THEN 'HOLD'
        ELSE 'RELEASED'
    END AS HOLD_STATUS,
    CASE
        WHEN h.RELEASETXNDATE IS NULL THEN
            ROUND((SYSDATE - h.HOLDTXNDATE) * 24, 2)
        ELSE
            ROUND((h.RELEASETXNDATE - h.HOLDTXNDATE) * 24, 2)
    END AS HOLD_HOURS
FROM DWH.DW_MES_HOLDRELEASEHISTORY h
LEFT JOIN DWH.DW_MES_CONTAINER c
  ON c.CONTAINERID = h.CONTAINERID
WHERE h.CONTAINERID = :container_id
ORDER BY h.HOLDTXNDATE DESC
