Joining 2 tables based on Dynamic criteria
Joining (or the expression in SELECT) looks similar to:

FROM #A 
FULL OUTER JOIN #PS ON #A.ACCOUNT_NUMBER = #PS.ACCOUNT_CODE__C 
                   AND (    (#A.ID = #PS.I AND INSTR(#A.Id_Type, 'I') &gt; 0)
                         OR (#A.ID = #PS.T AND INSTR(#A.Id_Type, 'T') &gt; 0)
                         OR (#A.ID = #PS.C AND INSTR(#A.Id_Type, 'C') &gt; 0) )


The rest is trivial.