Pregunta

¿Cuál es la forma más sencilla de eliminar registros con nombre duplicado en una tabla? Las respuestas que encontré son muy confusas.

Relacionado:

  

Eliminando registros duplicados de la tabla

¿Fue útil?

Solución

¡Lo tengo! Simple y funcionó muy bien.

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

Otros consejos

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top