我需要从交集表中删除一些重复的条目。
这个表设置得非常糟糕,没有主键,所以我在删除重复的条目时遇到了一些麻烦。
以下是对该表的粗略概述:
col1 col2
------------
1 70
1 70
1 71
两个列都带有id,并且重复打破了东西。
答案 0 :(得分:5)
您可以使用RANKING Functions
with cte as
(
select row_number() over(partition by col1,col2 order by col1,col2 )as rowNum
from tableName
)
delete from cte where rowNum>1
答案 1 :(得分:1)
drop table #t
create table #t(col1 int,col2 int)
insert into #t values(1,70),(1,70),(2,71)
;with cte as
(
select [col1],[col2],rn=row_number() over(partition by col1 order by col2) from #t
)
delete from cte where rn>1
select * from #t
答案 2 :(得分:1)
with t1dups (col1, coldups)
AS (
select col2, ROW_NUMBER() Over (Partition by col1, col2 order by col2) as dups from t1 )
delete from t1dups where coldups > 1