zoukankan      html  css  js  c++  java
  • 整理删除重复数据的语句

    开场白:前边一段为前期数据准备

    后边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
    **********初心不改,方得始终。
  • 相关阅读:
    Codeforces 741D 【Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths】
    Codeforces 235C 【Cyclical Quest】
    UOJ #62.【UR #5】怎样跑得更快
    luoguP3648 [APIO2014]序列分割
    luoguP4782 [模板]2-SAT问题
    原博客已废弃
    个数可变的参数收藏
    新的一年开始。
    文件上传下载总结
    模板模式学习(转)
  • 原文地址:https://www.cnblogs.com/salv/p/11764067.html
Copyright © 2011-2022 走看看