sql 刪除重複資料的方法

2021-08-29 02:26:39 字數 1132 閱讀 3956

--建立表 並在表中新增重複資料

create table [dbo].[repeat](

[emp_no] [nvarchar](50) null,

[name] [nvarchar](50) null,

[age] [int] null

) on [primary]

-- 插入測試資料

insert into repeat values('000','zhangsan',20);

insert into repeat values('001','weimeng',22);

insert into repeat values('002','weimeng',22);

insert into repeat values('003','liu',23);

insert into repeat values('004','liu',23);

insert into repeat values('005','liu',23);

insert into repeat values('006','li',24);

insert into repeat values('007','li',24);

insert into repeat values('008','li',24);

insert into repeat values('009','li',24);

select distinct *  into #tmp from repeat  --過濾非重複的資料到臨時表

delete from repeat --刪除重複表裡的所有資料

insert into repeat  select * from #tmp --把臨時表的資料 插入到表裡

drop table #tmp

alter table repeat add rowno int identity(1,1); --新增自增長列

delete from repeat where (select count(*) from repeat r where r.name=repeat.name and r.rowno < repeat.rowno ) > 0;

alter table repeat drop column rowno ;--刪除自增長列

sql刪除重複資料

1 建立表 create table dbo test id numeric 18,0 identity 1,1 not null primary key,name varchar 200 collate chinese prc ci as null remark varchar 1024 coll...

sql刪除重複資料

sql刪除重複資料 如果該錶需要刪除重複的記錄 重覆記錄保留1條 可以按以下方法刪除 select distinct into tmp from tablename drop table tablename select into tablename from tmp drop table tmp ...

SQL刪除重複資料

這個應用場景也不多說了 with c as select orderid,row number over partition by orderid order by select null as nfrom sales.myorders delete from c where n 1 利用row n...