patternsqlModerate
SQLite3: Remove duplicates
Viewed 0 times
sqlite3duplicatesremove
Problem
Given the following table
I'd like to delete all rows where
CREATE TABLE `sms` (
`_id` INTEGER,
`thread_id` INTEGER,
`address` TEXT,
`person` INTEGER,
`date` INTEGER,
`protocol` INTEGER,
`read` INTEGER DEFAULT 0,
`status` INTEGER DEFAULT -1,
`type` INTEGER,
`reply_path_present` INTEGER,
`subject` TEXT,
`body` TEXT,
`service_center` TEXT,
PRIMARY KEY(_id)
);I'd like to delete all rows where
address and body are identical. In other words, eliminate dups based on address and body, leaving only the first occurrence.Solution
If you just want to disambiguate two rows with similar content, you can use the ROWID functionality in SQLite3, which helps uniquely identify each row in the table.
Something like this:
should work to get you the rows with the minimum rowid, which will be the first occurrence of that row in the table.
Be sure to run this in a transaction however, to make sure that it does exactly what you want, and of course, take a backup beforehand. =)
Something like this:
DELETE FROM sms WHERE rowid NOT IN (SELECT min(rowid) FROM sms GROUP BY address, body);should work to get you the rows with the minimum rowid, which will be the first occurrence of that row in the table.
Be sure to run this in a transaction however, to make sure that it does exactly what you want, and of course, take a backup beforehand. =)
Context
StackExchange Database Administrators Q#116868, answer score: 16
Revisions (0)
No revisions yet.