code for history query