Replace temp with query