Domanda

Qual è il modo più semplice per eliminare i record con un nome duplicato in una tabella? Le risposte che ho trovato sono molto confuse.

Related:

  

Rimozione di record duplicati dalla tabella

È stato utile?

Soluzione

Ho capito! Semplice e ha funzionato alla grande.

delete 
   t1 
from 
   tTable t1, tTable t2 
where 
   t1.locationName = t2.locationName and  
   t1.id > t2.id 

http://www.cryer.co.uk/brian/sql /sql_delete_duplicates.htm

Altri suggerimenti

SQL Server 2005:

with FirstKey
AS
(
    SELECT MIN(ID), Name, COUNT(*) AS Cnt
      FROM YourTable
     GROUP BY Name
     HAVING COUNT(*) > 1
)
DELETE YourTable
  FROM YourTable YT
  JOIN FirstKey FK ON FK.Name = YT.Name AND FK.ID != YT.ID
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top