SELECT A.EMPLID , C.EMPL_RCD , A.COUNTRY , A.NATIONAL_ID_TYPE , CASE WHEN A.NATIONAL_ID_TYPE = 'CPF' THEN %Substring(A.NATIONAL_ID , 1, 7) ELSE A.NATIONAL_ID END , CASE WHEN A.NATIONAL_ID_TYPE = 'CPF' THEN '0' %Concat %Substring(A.NATIONAL_ID , 8 , 4) WHEN A.NATIONAL_ID_TYPE = 'CTPS' THEN CASE WHEN B.CTPS_SERIES_BRA IS NULL THEN ' ' ELSE B.CTPS_SERIES_BRA END ELSE ' ' END , CASE WHEN A.NATIONAL_ID_TYPE = 'CPF' THEN D.STATE WHEN A.NATIONAL_ID_TYPE = 'CTPS' THEN CASE WHEN B.CTPS_STATE_BRA IS NULL THEN ' ' ELSE B.CTPS_STATE_BRA END ELSE ' ' END FROM PS_JOB C , PS_ESTAB_TBL D , PS_PERS_NID A LEFT OUTER JOIN PS_PERSON_BRA B ON B.EMPLID = A.EMPLID WHERE A.COUNTRY = 'BRA' AND (A.NATIONAL_ID_TYPE <> 'CPF' OR (A.NATIONAL_ID_TYPE = 'CPF' AND NOT EXISTS ( SELECT 'X' FROM PS_PERS_NID A1 WHERE A1.EMPLID = A.EMPLID AND A1.COUNTRY = A.COUNTRY AND A1.NATIONAL_ID_TYPE = 'CTPS'))) AND C.EMPLID = A.EMPLID AND C.ACTION = 'HIR' AND D.ESTABID = C.ESTABID AND D.EFFDT = ( SELECT MAX(D1.EFFDT) FROM PS_ESTAB_TBL D1 WHERE D1.ESTABID = D.ESTABID AND D1.EFFDT <= C.EFFDT)
|