SELECT COMPANY ,CALENDAR_YEAR , EMPLID ,SUM(W2_AMOUNT) FROM PS_YE_AMOUNTS WHERE TAXFORM_ID IN ('I' ,'M','P','S','U','W') AND BOX = '01' AND EMPLID NOT IN ( SELECT DISTINCT B.W2C_ADD_EMPLID FROM PS_YE_W2C_AMOUNTS B WHERE B.BOX = '01' AND B.CALENDAR_YEAR = PS_YE_AMOUNTS.CALENDAR_YEAR AND W2C_BATCH_NBR = ( SELECT MAX(A.W2C_BATCH_NBR) FROM PS_YE_W2C_AMOUNTS A WHERE A.W2C_ADD_EMPLID = B.W2C_ADD_EMPLID AND A.BOX= B.BOX AND A.CALENDAR_YEAR= B.CALENDAR_YEAR)) GROUP BY COMPANY ,CALENDAR_YEAR , EMPLID UNION SELECT B.COMPANY ,B.CALENDAR_YEAR , B.W2C_ADD_EMPLID ,SUM(B.CURRENT_AMOUNT) FROM PS_YE_W2C_AMOUNTS B WHERE B.BOX = '01' AND W2C_BATCH_NBR = ( SELECT MAX(A.W2C_BATCH_NBR) FROM PS_YE_W2C_AMOUNTS A WHERE A.W2C_ADD_EMPLID = B.W2C_ADD_EMPLID AND A.BOX= B.BOX AND A.CALENDAR_YEAR= B.CALENDAR_YEAR) GROUP BY B.COMPANY ,B.CALENDAR_YEAR , B.W2C_ADD_EMPLID
|