sqlserver清除完全重復(fù)的數(shù)據(jù)只保留重復(fù)數(shù)據(jù)中的第一條
發(fā)布日期:2021-12-28 01:32 | 文章來(lái)源:源碼中國(guó)
--創(chuàng)建測(cè)試表 CREATE TABLE [dbo].[testtab]( [id] [nchar](10) NULL, [name] [nchar](10) NULL ) ; --向測(cè)試表插入測(cè)試數(shù)據(jù) insert into testtab values('1','1'); insert into testtab values('1','1'); insert into testtab values('2','2'); insert into testtab values('2','2'); insert into testtab values('3','3'); insert into testtab values('3','3'); --創(chuàng)建臨時(shí)表并向臨時(shí)表中插入測(cè)試表testtab中數(shù)據(jù)以及添加自增id:autoID select identity(int,1,1) as autoID, * into #Tmp from testtab --根據(jù)autoID刪除臨時(shí)表#tmp中的重復(fù)數(shù)據(jù),只保留每組重復(fù)數(shù)據(jù)中的第一條 delete #Tmp where autoID in(select max(autoID) from #Tmp group by id); --清除testtab表中的所有數(shù)據(jù) delete testtab; --向testtab表中插入#Tmp表中被處理過(guò)的數(shù)據(jù) insert into testtab select id,name from #Tmp; --刪除臨時(shí)表#Tmp drop table #Tmp;
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章