SELECT A.BUSINESS_UNIT , A.VOUCHER_ID , A.CONVER_HDR_SEQ_NUM , B.CONVER_LN_SEQ_NUM , A.CONVER_HDR_DTTM , A.EM_MESSAGE_TOPIC , A.OPRID_FROM , A.OPRID_TO , B.OPRID , B.CONVER_LN_DTTM FROM PS_EM_CONVER_HDR A , PS_EM_CONVER_LINE B WHERE A.BUSINESS_UNIT = B.BUSINESS_UNIT AND A.VOUCHER_ID = B.VOUCHER_ID AND A.EM_TOPIC_STATUS = 'O' AND A.CONVER_HDR_SEQ_NUM = B.CONVER_HDR_SEQ_NUM AND B.CONVER_LN_SEQ_NUM = ( SELECT MAX(C1.CONVER_LN_SEQ_NUM) FROM PS_EM_CONVER_LINE C1 WHERE C1.BUSINESS_UNIT = B.BUSINESS_UNIT AND C1.VOUCHER_ID = B.VOUCHER_ID AND C1.CONVER_HDR_SEQ_NUM = B.CONVER_HDR_SEQ_NUM ) AND A.OPRID_TO <> ' ' AND ((EXISTS ( SELECT 'X' FROM PS_VCHR_HDR_QV QV WHERE QV.BUSINESS_UNIT = A.BUSINESS_UNIT AND QV.VOUCHER_ID = A.VOUCHER_ID)) OR (EXISTS ( SELECT 'X' FROM PS_VOUCHER VCHR , PS_PYMNT_VCHR_XREF XREF WHERE VCHR.BUSINESS_UNIT = A.BUSINESS_UNIT AND VCHR.VOUCHER_ID = A.VOUCHER_ID AND VCHR.BUSINESS_UNIT = XREF.BUSINESS_UNIT AND VCHR.VOUCHER_ID = XREF.VOUCHER_ID AND XREF.PYMNT_SELCT_STATUS = 'N'))) UNION SELECT A2.BUSINESS_UNIT , A2.VOUCHER_ID , A2.CONVER_HDR_SEQ_NUM , B2.CONVER_LN_SEQ_NUM , A2.CONVER_HDR_DTTM , A2.EM_MESSAGE_TOPIC , A2.OPRID_FROM , C.OPRID , B2.OPRID , B2.CONVER_LN_DTTM FROM PS_EM_CONVER_HDR A2 , PS_EM_CONVER_LINE B2 , PS_EM_PYM_DISP_USR C WHERE A2.BUSINESS_UNIT = B2.BUSINESS_UNIT AND A2.VOUCHER_ID = B2.VOUCHER_ID AND A2.EM_TOPIC_STATUS = 'O' AND A2.CONVER_HDR_SEQ_NUM = B2.CONVER_HDR_SEQ_NUM AND (B2.OPRID = A2.OPRID_FROM OR B2.OPRID = C.OPRID) AND B2.CONVER_LN_SEQ_NUM = ( SELECT MAX(C2.CONVER_LN_SEQ_NUM) FROM PS_EM_CONVER_LINE C2 WHERE C2.BUSINESS_UNIT = B2.BUSINESS_UNIT AND C2.VOUCHER_ID = B2.VOUCHER_ID AND C2.CONVER_HDR_SEQ_NUM = B2.CONVER_HDR_SEQ_NUM ) AND A2.OPRID_TO = ' ' AND A2.BUSINESS_UNIT = C.BUSINESS_UNIT AND A2.VOUCHER_ID = C.VOUCHER_ID AND A2.CONVER_HDR_SEQ_NUM = C.CONVER_HDR_SEQ_NUM
|