SELECT A.EMPLID ,A.EMPL_RCD ,A.GPGB_LOAN_ID ,A.GPGB_TAX_END_YR ,A.PYMT_DT ,A.GPGB_AMT_BAL FROM PS_GPGB_LOAN_WA A WHERE EXISTS ( SELECT B.GPGB_P11D_REPORT FROM PS_GPGB_EE_LOAN_IN B WHERE B.EMPLID = A.EMPLID AND B.EMPL_RCD = A.EMPL_RCD AND B.GPGB_LOAN_ID = A.GPGB_LOAN_ID AND B.GPGB_P11D_REPORT = 'Y') OR (NOT EXISTS ( SELECT C.GPGB_P11D_REPORT FROM PS_GPGB_EE_LOAN_IN C WHERE C.EMPLID = A.EMPLID AND C.EMPL_RCD = A.EMPL_RCD AND C.GPGB_LOAN_ID = A.GPGB_LOAN_ID AND C.GPGB_P11D_REPORT = 'Y') AND EXISTS ( SELECT D.GPGB_P11D_REPORT FROM PS_GPGB_EE_LOAN_DT D WHERE D.EMPLID = A.EMPLID AND D.EMPL_RCD = A.EMPL_RCD AND D.GPGB_LOAN_ID = A.GPGB_LOAN_ID AND D.GPGB_P11D_REPORT = 'Y' AND D.EFFDT = ( SELECT MAX(E.EFFDT) FROM PS_GPGB_EE_LOAN_DT E WHERE E.EMPLID = A.EMPLID AND E.EMPL_RCD = A.EMPL_RCD AND E.GPGB_LOAN_ID = A.GPGB_LOAN_ID AND E.EFFDT <= A.PYMT_DT)))
|