MSSQL 删除重复数据

--删除重复数据
if object_id(N'test',N'U') is not null 
  drop table test
go
create table test(  
  id INT,
  n  NVARCHAR(20)
)
go 
INSERT INTO test VALUES (1,'a')
INSERT INTO test VALUES (1,'b')
INSERT INTO test VALUES (2,'c')
go
with t as 
(
  select ROW_NUMBER( )  
    --按照id分组,删除id重复数据
      OVER(partition by id order by id, n) as rowid, * from test
)
--删除
delete from t where rowid>1
--查看原数据
select * from test

 

你可能感兴趣的:(MSSQL 删除重复数据)