Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete from where count greater than 1, but keep a row

Tags:

sql

sql-server

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>
like image 832
user2124871 Avatar asked Feb 28 '14 19:02

user2124871


People also ask

What happens if you omit the WHERE clause in the delete statement?

If you omit the WHERE clause from a DELETE statement, SQL removes all the rows from the table.

Can delete have WHERE clause?

You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the records would be deleted.

How do I remove duplicate records from the table with one copy?

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.


1 Answers

Try this

DELETE FROM [table] WHERE id NOT IN (
    SELECT MAX(id)
    FROM [table]
    GROUP BY url
     )
like image 176
Avt Avatar answered Sep 22 '22 23:09

Avt