整理删除重复数据的语句
Posted salv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整理删除重复数据的语句相关的知识,希望对你有一定的参考价值。
开场白:前边一段为前期数据准备
后边t-sql为筛选出重复的行数-1,即为需要删除的数据,其中guid必须唯一
--前期准备 SELECT ProviderGUID,bankaccounts,count(1) as bum INTO #temp1 from p_ProviderBanks group by ProviderGUID,bankaccounts having count(1)>1 SELECT rowid=IDENTITY(INT,1,1),flag=0, providerguid,a.ProviderBankGUID,a.BankAccounts INTO #temp2 FROM p_ProviderBanks a WHERE EXISTS (SELECT * FROM #temp1 WHERE a.ProviderGUID=ProviderGUID AND a.BankAccounts=BankAccounts) SELECT TOP (10-1) * FROM #temp1 SELECT rowid=IDENTITY(INT,1,1),flag=0,providerguid,BankAccounts,bum INTO #temp3 FROM #temp1 SELECT * FROM #temp2 SELECT * FROM #temp3 DROP TABLE #temp3 --删除重复数据 BEGIN SET NOCOUNT ON DECLARE @num INT DECLARE @rowid INT DECLARE @providerguid UNIQUEIDENTIFIER DECLARE @bankaccounts VARCHAR(400) SELECT @rowid=MIN(rowid) FROM #temp3 WHERE flag=0 DECLARE @strsql NVARCHAR(200) --CREATE TABLE erpbak.dbo.tempdelete --( --providerbankguid UNIQUEIDENTIFIER --) PRINT @rowid WHILE @rowid is not NULL BEGIN SELECT @providerguid=ProviderGUID,@bankaccounts=BankAccounts,@num=bum FROM #temp3 WHERE rowid=@rowid SET @num=@num-1 --PRINT @bankaccounts --PRINT @num SET @strsql=‘ insert into erpbak.dbo.tempdelete SELECT TOP ‘+CAST( @num AS VARCHAR(5) )+‘ ProviderBankGUID FROM #temp2 WHERE BankAccounts=‘‘‘+CAST(@bankaccounts AS VARCHAR(100))+‘‘‘ AND ProviderGUID=‘‘‘+CAST(@providerguid AS VARCHAR(100))+‘‘‘‘ --PRINT @strsql EXEC sp_executesql @strsql UPDATE #temp3 SET flag=1 WHERE rowid=@rowid SELECT @rowid=MIN(rowid) FROM #temp3 WHERE flag=0 END SET NOCOUNT OFF END
以上是关于整理删除重复数据的语句的主要内容,如果未能解决你的问题,请参考以下文章