Fix for issue creating correct join SQL where there are multiple joins to the same table but on different columns