I am looking for an SQL query to select all records not selected by another query on the same table. Specifically I want to select all records which have duplicates of a pa
Specifically I want to select all records which have duplicates of a particular field('fieldA') and then delete all but one of those records.
In that case, join it:
delete x
from myTable x
join myTable z on x.field = z.field
where x.id > z.id
If you change your not IN sub SELECT statement to only return one column (ID), it should work -
http://blog.sqlauthority.com/2008/04/22/sql-server-better-performance-left-join-or-not-in/
Something like:
SELECT id FROM table WHERE id IN (
SELECT MIN(id) FROM table GROUP BY fieldA HAVING COUNT(*) > 1
)