use tempdbgodeclare @testtable table (id int, value int, name char(4))insert into @testtable values(1, 10, 'test'), (2, 5, 'prod'), (3, 10, 'test'), (4, 4, 'test'), (5, 4, 'test'), (6, 10, 'test')select * from @testtabledelete from @testtableWHERE id in (select t2.id from @testtable t1 INNER JOIN @testtable t2 ON t1.id = t2.id - 1 INNER JOIN @testtable t3 ON t2.id = t3.id-1WHERE t1.name = t2.name and t2.name = t3.name)select * from @testtable
DELETE a FROM @testtable aWHERE EXISTS (SELECT 1 FROM @testtable b WHERE (a.id = b.id+1 OR a.id = b.id-1) AND a.NAME = b.NAME AND a.value = b.value);
DECLARE @testtable TABLE (id INT, value INT, NAME CHAR(4));INSERT INTO @testtableVALUES (1, 10, 'test'), (2, 5, 'prod'), (3, 10, 'test'), (4, 4, 'test'), (5, 4, 'test'), (6, 10, 'test');
id value NAME----------- ----------- ----1 10 test2 5 prod3 10 test6 10 test
DELETE aFROM @testtable aINNER JOIN (SELECT id, value, NAME, ROW_NUMBER() OVER(PARTITION BY value ORDER BY id) AS rownum FROM @testtable) b ON a.id = b.idWHERE b.rownum > 1;
id value NAME----------- ----------- ----1 10 test2 5 prod4 4 test
DELETEFROM @testtableWHERE id IN (SELECT t2.id FROM @testtable t1 INNER JOIN @testtable t2 ON t1.id = t2.id - 1 INNER JOIN @testtable t3 ON t2.id = t3.id - 1 WHERE t1.NAME = t2.NAME AND t2.NAME = t3.NAME );
DECLARE @testtable TABLE (id INT, value INT, NAME CHAR(4));INSERT INTO @testtableVALUES (1, 10, 'test'), --Keep(2, 5, 'prod'), --Keep(4, 10, 'test'), --Keep (6, 4, 'test'), --Keep(7, 4, 'test'), --Drop(9, 4, 'Job'), --Keep (matching value but not name)(11, 10, 'test'), --Keep(13, 10, 'test') --Drop;with t1 as ( select ROW_NUMBER()over(order by id) RowID ,ID ,Name ,Value from @testtable ) delete a from @testtable a where id in ( select t1.id from t1 inner join t1 t2 on t1.RowID = t2.RowID + 1 and t1.value = t2.value and t1.NAME = t2.NAME )select *from @testtable