zoukankan      html  css  js  c++  java
  • 通过SQL语句删除重复记录

    最近在项目里碰到很多重复记录的事情,因为是设计业务里的具体数值,所以不敢轻易全部删除,数据量也很大,所以就倒腾了点代码在SQL里进行删除

    纯属菜鸟,还望高手指教啊

     1 --创建临时表
    2
    3 ifexists (select*from tempdb.dbo.sysobjects where id=object_id( 'tempdb.dbo.##TEMP'))
    4 droptable ##TEMP
    5
    6 --将有重复记录的数据导入到临时表里,并增添一个字增列
    7 selectIDENTITY(int,1,1) as ppkey,ParameterRelatedId,BusinessParameterID into ##Tempfrom ParameterRelated
    where RegionID='7E01D028-F84A-4D9A-9428-F68989215C0B'and YearTime=2011
    and BusinessParameterID in (select BusinessParameterID from BusinessParameters)
    8
    9 --分组后根据自增列的大小取出重复记录的第一个记录ID
    10 select*from ##Tempwhere ppkey in (selectMin(ppkey) from ##Tempgroupby BusinessParameterID)
    11
    12 --根据取出来的记录ID过滤,删除掉那些重复记录自增列不是最小的记录
    13 deletefrom ParameterRelated where ParameterRelatedId
    notin(select ParameterRelatedId from ##Tempwhere ppkey in
     (selectMin(ppkey) from ##Tempgroupby BusinessParameterID)) 
    and RegionID='7E01D028-F84A-4D9A-9428-F68989215C0B'and YearTime=2011
  • 相关阅读:
    Markdown常用语法
    课程及资源链接
    cpa-会计
    高管具备的能力
    JDK并发包-copy
    Guava工具类学习-copy
    java开发注解大全-copy
    Codeforces Round #703 (Div. 2)
    [ABC200E] Minflip Summation
    Codeforces Round #720 (Div. 2)
  • 原文地址:https://www.cnblogs.com/MrWby/p/2176042.html
Copyright © 2011-2022 走看看