DELETE FROM MyTableLEFT OUTER JOIN (SELECT MIN(RowId) as RowId, Col1, Col2, Col3FROM MyTableGROUP BY Col1, Col2, Col3) as KeepRows ONMyTable.RowId = KeepRows.RowIdWHEREKeepRows.RowId IS NULL
DELETE FROM MyTable WHERE NOT RowID IN(SELECT(SELECT TOP 1 RowID FROM MyTable mt2WHERE mt2.Col1 = mt.Col1AND mt2.Col2 = mt.Col2AND mt2.Col3 = mt.Col3)FROM MyTable mt)
-- given a table stories(story_id int not null primary key, story varchar(max) not null)CREATE TRIGGER prevent_plagiarismON storiesafter INSERT, UPDATEASDECLARE @cnt AS INT
SELECT @cnt = Count(*)FROM storiesINNER JOIN insertedON ( stories.story = inserted.storyAND stories.story_id != inserted.story_id )
IF @cnt > 0BEGINRAISERROR('plagiarism detected',16,1)
ROLLBACK TRANSACTIONEND
;
--Ensure that any immediately preceding statement is terminated with a semicolon aboveWITH cteAS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3ORDER BY ( SELECT 0)) RNFROM #MyTable)DELETE FROM cteWHERE RN > 1;
DELETE FROM TableNameWHERE ID NOT IN (SELECT MAX(ID)FROM TableNameGROUP BY Column1,Column2,Column3/*Even if ID is not null-able SQL Server treats MAX(ID) as potentiallynullable. Because of semantics of NOT IN (NULL) including the clausebelow can simplify the plan*/HAVING MAX(ID) IS NOT NULL)
DELETE FROM testing WHERE empno not IN (SELECT empno FROM (SELECT empno, ROW_NUMBER() OVER (PARTITION BY empno ORDER BY empno)AS [ItemNumber] FROM testing) a WHERE ItemNumber > 1)or empname not in(select empname from (select empname,row_number() over(PARTITION BY empno ORDER BY empno)AS [ItemNumber] FROM testing) a WHERE ItemNumber > 1)
CREATE TABLE car(Id int identity(1,1), PersonId int, CarId int)
INSERT INTO car(PersonId,CarId)VALUES(1,2),(1,3),(1,2),(2,4)
--SELECT * FROM car
;WITH CTE as(SELECT ROW_NUMBER() over (PARTITION BY personid,carid order by personid,carid) as rn,Id,PersonID,CarId from car)
DELETE FROM car where Id in(SELECT Id FROM CTE WHERE rn>1)
begin transaction-- create temp table with identical structure as source tableSelect * Into #temp From tableName Where 1 = 2
-- insert distinct values into tempinsert into #tempselect distinct *from tableName
-- delete from sourcedelete from tableName
-- insert into source from tempinsert into tableNameselect *from #temp
rollback transaction-- if this works, change rollback to commit and execute again to keep you changes!!
--DELETE FROM table1--WHERE id IN (SELECT MIN(id) FROM table1GROUP BY col1, col2, col3-- could add a WHERE clause here to further filterHAVING count(*) > 1--)
with MYCTE as (SELECT ROW_NUMBER() OVER (PARTITION BY DuplicateKey1,DuplicateKey2 -- optionalORDER BY CreatedAt -- the first row among duplicates will be kept, other rows will be removed) RNFROM MyTable)DELETE FROM MYCTEWHERE RN > 1
WITH tblTemp as(SELECT ROW_NUMBER() Over(PARTITION BY Name,Department ORDER BY Name)As RowNumber,* FROM <table_name>)DELETE FROM tblTemp where RowNumber >1
alter table MyTable add sno int identity(1,1)delete from MyTable where sno in(select sno from (select *,RANK() OVER ( PARTITION BY RowID,Col3 ORDER BY sno DESC )rankFrom MyTable)Twhere rank>1)
alter table MyTabledrop column sno
CREATE PROCEDURE sp_DeleteDuplicate @tableName varchar(100), @DebugMode int =1ASBEGINSET NOCOUNT ON;
IF(OBJECT_ID('tempdb..#tableMatrix') is not null) DROP TABLE #tableMatrix;
SELECT ROW_NUMBER() OVER(ORDER BY name) as rn,name into #tableMatrix FROM sys.columns where [object_id] = object_id(@tableName) ORDER BY name
DECLARE @MaxRow int = (SELECT MAX(rn) from #tableMatrix)IF(@MaxRow is null)RAISERROR ('I wasn''t able to find any columns for this table!',16,1)ELSEBEGINDECLARE @i int =1DECLARE @Columns Varchar(max) ='';
WHILE (@i <= @MaxRow)BEGINSET @Columns=@Columns+(SELECT '['+name+'],' from #tableMatrix where rn = @i)
SET @i = @i+1;END
---DELETE LAST commaSET @Columns = LEFT(@Columns,LEN(@Columns)-1)
DECLARE @Sql nvarchar(max) = 'WITH cteRowsToDelteAS (SELECT ROW_NUMBER() OVER (PARTITION BY '+@Columns+' ORDER BY ( SELECT 0)) as rowNumber,* FROM '+@tableName+')
DELETE FROM cteRowsToDelteWHERE rowNumber > 1;'SET NOCOUNT OFF;IF(@DebugMode = 1)SELECT @SqlELSEEXEC sp_executesql @SqlENDEND
因此,如果您创建这样的表:
IF(OBJECT_ID('MyLitleTable') is not null)DROP TABLE MyLitleTable
CREATE TABLE MyLitleTable(A Varchar(10),B money,C int)---------------------------------------------------------
INSERT INTO MyLitleTable VALUES('ABC',100,1),('ABC',100,1), -- only this row should be deleted('ABC',101,1),('ABC',100,2),('ABCD',100,1)
-----------------------------------------------------------
exec sp_DeleteDuplicate 'MyLitleTable',0
我认为这会很有帮助。在这里,ROW_NUMBER()OVER(PARTITION BY res1. title ORDER BY res1. Id)作为num被用来区分重复的行。
delete FROM(SELECT res1.*,ROW_NUMBER() OVER(PARTITION BY res1.Title ORDER BY res1.Id)as numFROM(select * from [dbo].[tbl_countries])as res1)as res2WHERE res2.num > 1
SELECT DISTINCT * INTO #TemNewTable FROM #OriginalTableTRUNCATE TABLE #OriginalTableINSERT INTO #OriginalTable SELECT * FROM #TemNewTableDROP TABLE #TemNewTable