fixed problem in SQL query that was keeping records from duplicating