replace-temp-with-query refactoring