modified sql code for rie records