zoukankan      html  css  js  c++  java
  • 部分关键字段重复的记录(保留最后记录)

    表名:tableName   字段 ID,CODE,LogTime

    数据如:

    ID,CODE,LogTime

    1,A1234,2011-09-02 13:12:10

    2,A1234,2011-09-03 13:12:10

    3,A1237,2011-09-04 13:12:10

    4,A1237,2011-09-04 13:12:10

    5,A1237,2011-09-05 13:12:10

    想要删除的数据

    ID,CODE,LogTime

    2,A1234,2011-09-03 13:12:10

    5,A1237,2011-09-05 13:12:10

    SQL:

    先找到想要的数据

    select Code,max(logtime) from tableName  group by Code

    哈哈。。。删除不了。

    从网上搜了一个如下:

    SQL Server

    select identity(int,1,1) as autoID, * into #Tmp from tableName;

    select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Address;

    drop table tableName;

    select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);

    drop table #Tmp;

    drop table #Tmp2;

    改成自己的如下:

    select identity(int,1,1) as autoID, * into #Tmp from #table1 Order by logtime;   //按时间大排序

    select maX(autoID) as autoID into #Tmp2 from #Tmp group by Code  //同一CODE的取最大的AUTOID

    drop table tableName;

    select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);

    drop table #Tmp;

    drop table #Tmp2;

    OVER!

  • 相关阅读:
    UVALive2678子序列
    UVA11549计算机谜题
    UVA11520填充正方形
    LA3635派
    UVALive3971组装电脑
    记录未完成题目
    SPOJ 6219 Edit distance字符串间编辑距离
    ACM组队安排-——杭电校赛(递推)
    逆袭指数-——杭电校赛(dfs)
    油菜花王国——杭电校赛(并查集)
  • 原文地址:https://www.cnblogs.com/dkongyying/p/2236451.html
Copyright © 2011-2022 走看看