added replace temp with query