以下是tsql查询:
DECLARE @table TABLE(data VARCHAR(20)) INSERT INTO @table VALUES ('not duplicate row') INSERT INTO @table VALUES ('duplicate row') INSERT INTO @table VALUES ('duplicate row') INSERT INTO @table VALUES ('second duplicate row') INSERT INTO @table VALUES ('second duplicate row') SELECT data INTO #duplicates FROM @table GROUP BY data HAVING COUNT(*) > 1 -- delete all rows that are duplicated DELETE FROM @table FROM @table o INNER JOIN #duplicates d ON d.data = o.data -- insert one row for every duplicate set INSERT INTO @table(data) SELECT data FROM #duplicates
我理解它在做什么,但逻辑的最后一部分(在每个重复集后插入一行)没有意义.我们有一组代码--delete所有重复的行,这样就可以删除重复项,那么最后一部分是什么?
此查询在此处找到
谢谢
我们有一组代码--delete所有重复的行,这样就可以删除重复项,那么最后一部分是什么?
首先,它删除所有重复的行.也就是说,所有行,原始也是.在上面的情况中,之后只有一行('not duplicate row'
)将保留在表中DELETE
.其他所有四行都将被删除.
然后再次使用已删除的行填充表,但现在删除了重复项.
这不是删除重复项的最佳方法.
最好的方法是:
WITH q AS ( SELECT data, ROW_NUMBER() OVER (PARTITION BY data ORDER BY data) AS rn FROM @table ) DELETE FROM q WHERE rn > 1