In that case, perhaps it would be best to do it using the EXISTS clause:select * from BIProd.dbo.D_Accounts X where exists(select * from iplan.dbo.upload_generalexpensetest a left outer join BIProd.dbo.D_Accounts bon a.LEID = b.LEIDand a.OBJ = b.L5Accountand a.SUB = b.L5Sub where a.LEID between @leid1 and @leid2 and( b.L5Account is null or b.L5Sub is null or b.LEID is null)and X.LEID = a.leid and X.l5account = a.obj)
Alternatively, you could use an inner join as shown below. Not sure which one will be more efficient.select * from BIProd.dbo.D_Accounts X inner join(select distinct a.leid,a.obj from iplan.dbo.upload_generalexpensetest a left outer join BIProd.dbo.D_Accounts bon a.LEID = b.LEIDand a.OBJ = b.L5Accountand a.SUB = b.L5Sub where a.LEID between @leid1 and @leid2 and( b.L5Account is null or b.L5Sub is null or b.LEID is null)) Y on X.leid = Y.LEID and x.l5account = Y.obj