rewrite sql to pull from new table