1 솔루션, 당신이 COL_A
및 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
두 번째 해결책 정말로 두 개의 col MY_TABLE에서 umns 당신은 사용할 수 있습니다
-- 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
나머지 열은 동일합니까? 어떤 것을 보관할 것인지 어떻게 결정합니까? – FJT
@FionaT,이 경우, 하나를 유지하는 한 계속 유지하지 않습니다. 이것은 모두 동일한 (COL_A, COL_B) 쌍을 가지고 있기 때문입니다. 간단히하기 위해 COL_A 및 COL_B 만 있습니다. – czchlong
다른 필드 (잘하면 키 필드)가없는 경우 delete 문은 모든 레코드를 삭제합니다. 레코드를 구별 할 수있는 다른 필드가 있어야합니다. –