如何从SQL Server中的表中删除重复记录?
答案 0 :(得分:2)
要删除列col_1, col_2, ... col_n
组合重复的行,可以使用公用表表达式;
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col1, ..., col_n ORDER BY col_1) AS rn
FROM table_1
)
DELETE FROM cte WHERE rn<>1;
由于行按列出的列的内容进行分类,如果行在所有方面都相同,您仍需要列出查询中的所有列。
与往常一样,在生产数据上从Internet上的随机用户中删除之前进行测试和/或备份。
答案 1 :(得分:1)
WITH CTE AS(
SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
答案 2 :(得分:0)
以下问题和给出的答案对您来说可能是最好的帮助
您可以选择rowId的最小值和最大值(如果有和身份字段,则另外添加一个)
DELETE MyTable
FROM MyTable
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, Col1, Col2, Col3
FROM MyTable
GROUP BY Col1, Col2, Col3
) as KeepRows ON
MyTable.RowId = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL
Use
答案 3 :(得分:0)
在表格中添加标识列:
Alter table tbl_name add Id int identity(1,1)
然后运行以下查询以从表中删除记录:
Delete from tbl_Name where Id not in(select min(Id) from tbl_Name group by RowId)
答案 4 :(得分:0)
//duplicate_data_table contains duplicate values
create temp as (select distinct * from duplicate_data_table);
drop duplicate_data_table;
create duplicate_data_table as (select * from temp);
drop temp;