3

可能重复:
删除 SQL Server 中的重复记录

我有一个表,其中唯一记录由复合键表示,例如 ( COL_A, COL_B)。

我已使用以下查询检查并确认我的表中有重复的行:

select COL_A, COL_B, COUNT(*)
from MY_TABLE
group by COL_A, COL_B
having count(*) > 1
order by count(*) desc

现在,我想删除所有重复的记录,但只保留一个。

有人可以阐明如何用 2 列实现这一目标吗?

编辑: 假设表只有COL_ACOL_B

4

2 回答 2

2

第一种解决方案,它很灵活,因为您可以添加比COL_Aand更多的列COL_B

-- create table with identity filed
-- using idenity we can decide which row we can delete
create table MY_TABLE_COPY
(
  id int identity,
  COL_A varchar(30), 
  COL_B  varchar(30)
  /*
     other columns
  */
)
go
-- copy data
insert into MY_TABLE_COPY (COL_A,COL_B/*other columns*/)
select COL_A, COL_B /*other columns*/
from MY_TABLE
group by COL_A, COL_B
having count(*) > 1
-- delete data from  MY_TABLE
-- only duplicates (!)
delete MY_TABLE
from MY_TABLE_COPY c, MY_TABLE t
where c.COL_A=t.COL_A 
and c.COL_B=t.COL_B
go
-- copy data without duplicates
insert into MY_TABLE (COL_A, COL_B /*other columns*/)
select t.COL_A, t.COL_B /*other columns*/
from MY_TABLE_COPY t
where t.id = (
               select max(id) 
               from MY_TABLE_COPY c  
               where t.COL_A = c.COL_A
               and  t.COL_B = c.COL_B
             ) 
go

第二种解决方案 如果您在 MY_TABLE 中确实有两列,则可以使用:

-- create table and copy data
select distinct COL_A, COL_B
into MY_TABLE_COPY
from MY_TABLE
-- delete data from  MY_TABLE 
-- only duplicates (!)
delete MY_TABLE
from MY_TABLE_COPY c, MY_TABLE t
where c.COL_A=t.COL_A 
and c.COL_B=t.COL_B
go
-- copy data without duplicates
insert into MY_TABLE
select t.COL_A, t.COL_B
from MY_TABLE_COPY t
go
于 2012-10-17T18:54:44.850 回答
1

尝试:

-- Copy Current Table
SELECT * INTO #MY_TABLE_COPY FROM MY_TABLE 

-- Delte all rows from current able
DELETE FROM MY_TABLE

-- Insert only unique values, removing your duplicates
INSERT INTO MY_TABLE
SELECT DISTINCT * FROM #MY_TABLE_COPY

-- Remove Temp Table
DROP TABLE #MY_TABLE_COPY

只要您在从 MY_TABLE 中删除行时不破坏任何外键,这应该可以工作。

于 2012-10-17T19:36:26.263 回答