Cleaning up the inconsistent sql