I have a table like this (SQL2008):
id url updated
1 http://url1.com null
2 http://url2.com <datetime>
3 http://url1.com <datetime>
I want to delete ONE of the url1.com records (preferably where updated is not null, but its fine if it doesnt work that way)
I have this query, which deletes all the duplicates - but doesn't keep a record (which is where i'm confused):
DELETE FROM [table] WHERE url IN (
SELECT url
FROM [table]
GROUP BY url
HAVING COUNT(*) > 1 )
How do I limit the delete? Result would ideally be:
id url updated
1 http://url1.com null
2 http://url2.com <datetime>
Right now it ends up like:
id url updated
2 http://url2.com <datetime>
If you omit the WHERE clause from a DELETE statement, SQL removes all the rows from the table.
You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the records would be deleted.
DELETE Duplicate Records Using ROWCOUNT So to delete the duplicate record with SQL Server we can use the SET ROWCOUNT command to limit the number of rows affected by a query. By setting it to 1 we can just delete one of these rows in the table.
Try this
DELETE FROM [table] WHERE id NOT IN (
SELECT MAX(id)
FROM [table]
GROUP BY url
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With