話不多說,請看**:
if not object_id('tempdb..#t') is null
drop table #t
gocreate table #t([id] int,[name] nvarchar(1),[memo] nvarchar(2))
insert #t
select 1,n'a',n'a1' union all
select 2,n'a',n'a2' union all
select 3,n'a',n'a3' union all
select 4,n'b',n'b1' union all
select 5,n'b',n'b2'
go--i、name相同id最小的記錄(推薦用1cmaail,2,3),保留最小一條
方法1:
delete a from #t a where exists(select 1 from #t where name=a.name and id
方cmaail法2:
delete a from #t a left join (select min(id)id,name from #t group by name) b on a.name=b.name and a.id=b.id where b.id is null
方法3:
delete a from #t a where id not in (s程式設計客棧elect min(id) from #t where name=a.name)
方法4(注:id為唯一時可用):
delete a from #t a where id not in(select min(id)from #t group by name)
方法5:
delete a from #t a where (select count(1) from where name=a.name and id0
方法6:
delete a from #t a where id<>(select top 1 id from #t where name=a.name order by id)
方法7:
delete a from #t a where id> id from #t where name=a.name)
select * from #t
本文標題: sql去除重覆記錄(七種)
本文位址:
SQL去除某欄位重覆記錄
sql去除某欄位重覆記錄 原理 對需要去重覆記錄的字段按組排序,然後取其中一條記錄。在總查詢語句中使用in語法過濾 去掉重覆記錄 select from company where comid in select max comid from company group by companyname...
SQL去除某欄位重覆記錄
原理 對需要去重覆記錄的字段按組排序,然後取其中一條記錄。在總查詢語句中使用in語法過濾 去掉重覆記錄 select from company where comid in select max comid from company group by companyname 得到重覆記錄數 sele...
SQL去除某欄位重覆記錄
原理 對需要去重覆記錄的字段按組排序,然後取其中一條記錄。在總查詢語句中使用in語法過濾 去掉重覆記錄 select from company where comid in select max comid from company group by companyname 得到重覆記錄數 sele...