I have a such statement:
SELECT MIN(ROWNUM) FROM my_table
GROUP BY NAME
HAVING COUNT(NAME) > 1);
This statement gives me the rownum
of the first duplicate, but when transform this statement into DELETE
it just delete everything. Why does it happen so?
This is because ROWNUM is a pseudo column which implies that they do not exist physically. You can better use
rowid
to delete the records.To remove the duplicates you can try like this:
Using
rownum
to delete duplicate records makes not much sense. If you need to delete duplicate rows, leaving only one row for each value ofname
, try the following:By adding further columns in
ORDER BY
clause, you can choice to delete the record with greatest/smallestID
, or some other field.