/*PJMES043-Hold歷史紀錄
  20240716 Peeler 新增匯總紀錄_PJM022024000878
  20250520 Peeler 加總判斷Future Hold不同站別相同原因只計算第一次Hold_PJM022025000733
*/
SELECT TO_NUMBER(BS.TXNDAY) AS TXNDAY1,BS.TXNDAY
       ,CASE WHEN :P_QCHOLDPARA = 2
             THEN SUM(CASE WHEN HD.HOLDTXNDAY <=  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS < HD.RELEASETXNDAY)  AND BS.TRANSACTION_DAYS <= TO_CHAR(SYSDATE , 'YYYY/MM/DD') AND HD.RN_HOLD = 1 THEN QTY
                           ELSE 0 END)
            ELSE SUM(CASE WHEN HD.HOLDTXNDAY <=  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS < HD.RELEASETXNDAY)  AND BS.TRANSACTION_DAYS <= TO_CHAR(SYSDATE , 'YYYY/MM/DD') AND QCHOLDFLAG = :P_QCHOLDPARA  AND HD.RN_HOLD = 1 THEN QTY
                          ELSE 0 END ) 
             END AS HOLDQTY
                            
        ,CASE WHEN :P_QCHOLDPARA = 2
              THEN SUM(CASE WHEN HD.HOLDTXNDAY =  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS <= HD.RELEASETXNDAY)  AND HD.FUTUREHOLD_FLAG = 1 THEN QTY
                            ELSE 0 END )
              ELSE SUM(CASE WHEN HD.HOLDTXNDAY =  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS <= HD.RELEASETXNDAY) AND QCHOLDFLAG = :P_QCHOLDPARA  AND HD.FUTUREHOLD_FLAG = 1 THEN QTY
                            ELSE 0 END )
              END AS NEW_HOLDQTY
              
        ,CASE WHEN :P_QCHOLDPARA = 2
              THEN SUM(CASE WHEN HD.RELEASETXNDAY =  BS.TRANSACTION_DAYS AND HD.RELEASETXNDAY >= HD.HOLDTXNDAY  THEN QTY
                            ELSE 0 END )
              ELSE SUM(CASE WHEN HD.RELEASETXNDAY =  BS.TRANSACTION_DAYS AND HD.RELEASETXNDAY >= HD.HOLDTXNDAY AND QCHOLDFLAG = :P_QCHOLDPARA  THEN QTY
                            ELSE 0 END )
              END AS RELEASEQTY
         
        ,CASE WHEN :P_QCHOLDPARA = 2
              THEN SUM(CASE WHEN HD.HOLDTXNDAY =  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS <= HD.RELEASETXNDAY)  AND HD.RN_HOLD = 1 AND HD.FUTUREHOLD_FLAG = 0 THEN QTY
                            ELSE 0 END )
              ELSE SUM(CASE WHEN HD.HOLDTXNDAY =  BS.TRANSACTION_DAYS AND (HD.RELEASETXNDAY IS NULL OR BS.TRANSACTION_DAYS <= HD.RELEASETXNDAY) AND QCHOLDFLAG = :P_QCHOLDPARA  AND HD.RN_HOLD = 1 AND HD.FUTUREHOLD_FLAG = 0 THEN QTY
                            ELSE 0 END )
              END AS FUTURE_HOLDQTY
      FROM(select   TO_CHAR(to_date(:P_TxnDate_S , 'YYYY/MM/DD') + rownum -1,'YYYY') AS TXNYEAR
                        , TO_CHAR(to_date(:P_TxnDate_S , 'YYYY/MM/DD') + rownum -1,'MM') AS TXNMONTH
                        , TO_CHAR(to_date(:P_TxnDate_S , 'YYYY/MM/DD') + rownum -1,'DD') AS TXNDAY
                        , TO_CHAR(to_date(:P_TxnDate_S , 'YYYY/MM/DD') + rownum -1,'YYYY/MM/DD') AS TRANSACTION_DAYS
                  From dual
                CONNECT BY LEVEL <=  TO_CHAR(LAST_DAY(to_date(:P_TxnDate_S,'YYYY/MM/DD')),'DD')
            )BS,
            (SELECT HOLDTXNDAY,RELEASETXNDAY,HOLDTXNDATE,RELEASETXNDATE,CONTAINERID,QTY, QCHOLDFLAG
                   ,ROW_NUMBER() OVER (PARTITION BY CONTAINERID,HOLDTXNDAY ORDER BY HOLDTXNDATE DESC) AS RN_HOLD--同一張工單當天重複Hold
                   ,ROW_NUMBER() OVER (PARTITION BY CONTAINERID,RELEASETXNDAY ORDER BY RELEASETXNDATE DESC) AS RN_RELEASE--同一張工單當天重複Release
                   ,CASE WHEN FUTUREHOLD = 1 AND RN_CONHOLD <> 1 THEN 0
                         ELSE 1 END AS FUTUREHOLD_FLAG --FutureHold相同原因第一筆計算1其餘給0
              FROM(SELECT CASE WHEN TO_CHAR(HD.HOLDTXNDATE,'HH24MI')>=0730
                                THEN TO_CHAR(HD.HOLDTXNDATE +1 ,'YYYY/MM/DD')
                                ELSE TO_CHAR(HD.HOLDTXNDATE ,'YYYY/MM/DD') END AS HOLDTXNDAY
                            ,CASE WHEN TO_CHAR(HD.RELEASETXNDATE,'HH24MI')>=0730
                                THEN TO_CHAR(HD.RELEASETXNDATE +1 ,'YYYY/MM/DD')
                                ELSE TO_CHAR(HD.RELEASETXNDATE ,'YYYY/MM/DD') END AS RELEASETXNDAY
                           ,HD.HOLDTXNDATE
                           ,HD.RELEASETXNDATE
                           ,HD.CONTAINERID
                           ,HD.QTY
                           ,CASE WHEN HD.HOLDREASONNAME IN(:P_QCHOLDREASON) THEN 1
                                 ELSE 0 END AS QCHOLDFLAG
                           ,CASE WHEN HD.FUTUREHOLDCOMMENTS IS NOT NULL THEN 1
                                 ELSE 0 END AS FUTUREHOLD
                           ,ROW_NUMBER() OVER (PARTITION BY HD.CONTAINERID,HD.HOLDREASONID ORDER BY HD.HOLDTXNDATE) AS RN_CONHOLD--同一張工單重複Hold
                      FROM  DW_MES_HOLDRELEASEHISTORY HD
                      WHERE 1=1
                       AND ((HD.HOLDTXNDATE >= TO_DATE(:P_TxnDate_S||' 073000', 'YYYYMMDD HH24MISS')-1) Or :P_TxnDate_S is null OR (HD.RELEASETXNDATE >= TO_DATE(:P_TxnDate_S||' 073000', 'YYYYMMDD HH24MISS')-1) OR (HD.RELEASETXNDATE IS NULL))
                       AND ((HD.HOLDTXNDATE <= TO_DATE(:P_TxnDate_E||' 073000', 'YYYYMMDD HH24MISS')) Or :P_TxnDate_E is null OR (HD.RELEASETXNDATE <= TO_DATE(:P_TxnDate_E||' 073000', 'YYYYMMDD HH24MISS')) OR (HD.RELEASETXNDATE IS NULL))
                  )     
             )HD 
         WHERE  1=1
GROUP BY  BS.TXNDAY