摘要:sql
下文將分享三種不一樣的數據去重方法
數據去重:需根據某一字段來界定,當此字段出現大於一行記錄時,咱們就界定爲此行數據存在重複。ide
數據去重方法1:
當表中最在最大流水號時候,咱們能夠經過關聯的方式爲每條重複的記錄獲取惟一值
數據去重方法2:
爲表中記錄,按照指定字段進行羣組,並獲取最大流水號,而後再進行去重操做
數據去重方法3:
採用分組後,重複數據組內排名,若是排名大於1表明是重複數據行數據
三種去重方法效率對比:
方法3 > 方法2 > 方法1
sqlserver
create table test(keyId int identity,sort varchar(10), info varchar(20)) go ---方法1 truncate table test ; insert into test(sort,info)values('A','maomao365.com')--1 insert into test(sort,info)values('A','貓貓小屋') --2 insert into test(sort,info)values('B','mssql_blog') --3 insert into test(sort,info)values('B','優秀的sql——blog') --4 insert into test(sort,info)values('B','maomao365') --5 insert into test(sort,info)values('C','sql優化blog') --6 go delete from test where test.keyId = (select max(b.keyId) from test b where test.sort=b.sort); select * from test ---方法2: truncate table test ; insert into test(sort,info)values('A','maomao365.com') insert into test(sort,info)values('A','貓貓小屋') insert into test(sort,info)values('B','mssql_blog') insert into test(sort,info)values('B','優秀的sql——blog') insert into test(sort,info)values('B','maomao365') insert into test(sort,info)values('C','sql優化blog') go delete from test where keyid not in(select min(keyId) from test group by sort having count(sort)>=1); select * from test ---方法3: truncate table test ; insert into test(sort,info)values('A','maomao365.com') insert into test(sort,info)values('A','貓貓小屋') insert into test(sort,info)values('B','mssql_blog') insert into test(sort,info)values('B','優秀的sql——blog') insert into test(sort,info)values('B','maomao365') insert into test(sort,info)values('C','sql優化blog') go delete A2 from ( select row_Number() over(partition by sort order by keyid) as keyId_e,* from test ) as A2 where A2.keyId_e >1 select * from test go drop table test
<img src="http://www.maomao365.com/wp-content/uploads/2018/07/mssql_sqlserver_數據表數據去重的三種方法分享.png" alt="mssql_sqlserver_數據表數據去重的三種方法分享" width="813" height="749" class="size-full wp-image-6767" />優化