SQL查询删除所有重复记录但只有一个

时间:2012-10-17 17:24:56

标签: tsql duplicates sybase duplicate-removal

  

可能重复:
  delete duplicate records in SQL Server

我有一张表,其中唯一记录由复合键表示,例如(COL_ACOL_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

2 个答案:

答案 0 :(得分:2)

第一个解决方案, 它非常灵活,因为您可以添加比COL_ACOL_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

答案 1 :(得分: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中删除行时没有破坏任何外键,这应该有效。