SELECT CASE WHEN D.MATURITY_DT IS NULL THEN (%CurrentDateIn - D.SETTLEMENT_DT) ELSE (D.MATURITY_DT - D.SETTLEMENT_DT) END ,D.TRANSACT_CURRENCY ,D.TRANSACTION_AMT ,M.MTM_ACCTG_VALUE FROM PS_TRX_HEADER_TR H INNER JOIN PS_TRX_DETAIL_TR D ON H.BUSINESS_UNIT = D.BUSINESS_UNIT AND H.TREAS_HEADER_ID = D.TREAS_HEADER_ID LEFT JOIN PS_RSK_DEAL_MTM M ON H.BUSINESS_UNIT = M.BUSINESS_UNIT AND H.TREAS_HEADER_ID = M.TREAS_HEADER_ID WHERE D.MATURITY_DT >= %CurrentDateIn AND D.INSTRMNT_BASE_TYPE IN ('01') AND D.ASSET_LIABILITY = 'A' AND H.DEAL_STATUS='03' AND D.SETTLEMENT_DT <= %CurrentDateIn AND (M.AS_OF_DATE IN (( SELECT MAX(MTM.AS_OF_DATE) FROM PS_RSK_DEAL_MTM MTM WHERE H.BUSINESS_UNIT = MTM.BUSINESS_UNIT AND H.TREAS_HEADER_ID = MTM.TREAS_HEADER_ID AND MTM.AS_OF_DATE <= %CurrentDateIn)) OR M.AS_OF_DATE IS NULL) UNION SELECT %CurrentDateIn - D.SETTLEMENT_DT ,D.TRANSACT_CURRENCY ,D.TRANSACTION_AMT ,M.MTM_ACCTG_VALUE FROM PS_TRX_HEADER_TR H INNER JOIN PS_TRX_DETAIL_TR D ON H.BUSINESS_UNIT = D.BUSINESS_UNIT AND H.TREAS_HEADER_ID = D.TREAS_HEADER_ID LEFT JOIN PS_RSK_DEAL_MTM M ON H.BUSINESS_UNIT = M.BUSINESS_UNIT AND H.TREAS_HEADER_ID = M.TREAS_HEADER_ID WHERE D.INSTRMNT_BASE_TYPE IN ('09') AND D.ASSET_LIABILITY = 'A' AND H.DEAL_STATUS='03' AND D.SETTLEMENT_DT <= %CurrentDateIn AND (M.AS_OF_DATE IN (( SELECT MAX(MTM.AS_OF_DATE) FROM PS_RSK_DEAL_MTM MTM WHERE H.BUSINESS_UNIT = MTM.BUSINESS_UNIT AND H.TREAS_HEADER_ID = MTM.TREAS_HEADER_ID AND MTM.AS_OF_DATE <= %CurrentDateIn)) OR M.AS_OF_DATE IS NULL)
|