If chargecode is in tableB, insert that filter on chargecode in the JOIN instead of in the WHERE clauseselect x.Number
,y.currencyCode,y.chargeAmount
into #withAAAchargecode
from tableA x
left join tableB y
on x.passengerid=y.passengerid
AND chargecode = 'AAA'