SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考。

1.如果有ID字段,就是具有唯一性的字段

delect   table  tableName  where   id   not   in   (   select   max(id)   from   table   group   by   col1,col2,col3...   )   

group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2. 如果是判断所有字段也可以这样  ,【对于表中的指定的字段的进行检查是否相同】

select   *   into   #temp  from   tablename    group   by   id1,id2,....

delete    tablename

insert   into   table  select   *   from   #temp

drop   table   #temp

3.  首先去重复,再获取N*1条数据插入到临时表中,【对于表中的所有字段的进行检查是否相同】,再将原表的数据删除,然后将临时表的数据插入到原表,最后删除临时表。

select   distinct   *   into   #temp   from   tablename

delete       tablename

go

insert   tablename   select   *   from   #temp

go

drop   table   #temp

4. 没有ID的情况

select   identity(int,1,1)   as   id,*   into   #temp   from   tabel

delect   #   where   id   not   in   (

select   max(id)   from   #   group   by   col1,col2,col3...)

delect   table

inset   into   table(...)

select   .....   from   #temp

5. col1+','+col2+','...col5 联合主键

select   *   from     table   where   col1+','+col2+','...col5   in   (

select   max(col1+','+col2+','...col5)   from   table

where   having   count(*)>1

group   by   col1,col2,col3,col4

)

group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

6.

select   identity(int,1,1)   as   id,*   into   #temp   from   tabel

select   *   from     #temp   where   id   in   (

select   max(id)   from   #emp   where   having   count(*)>1   group   by   col1,col2,col3...)

SQL Server删除重复行的6个方法的更多相关文章

  1. SQL查找删除重复行

    本文讲述如何查找数据库里重复的行.这是初学者十分普遍遇到的问题.方法也很简单.这个问题还可以有其他演变,例如,如何查找“两字段重复的行”(#mysql IRC 频道问到的问题) 如何查找重复行 第一步 ...

  2. SQL Server删除表信息的三种方法

    1.使用DELETE实现SQL Server删除表信息 (1)删除表中的全部信息 USE student GO DELETE student      --不加where条件,删除表中的所有记录 go ...

  3. SQL Server 删除重复记录,只保留一条记录

    原文地址:http://blog.csdn.net/eriato/article/details/17417303 有张表格之前没有设计关键字段的唯一约束,导致有时候执行插入操作时不小心执行了多次就出 ...

  4. SQL SERVER 合并重复行,行列转换

    引用自:http://www.cnblogs.com/love-summer/archive/2012/03/27/2419778.html sql server2000 里面如何实现oracle10 ...

  5. SQL Server 删除重复数据只保留一条

    DELETE FROM Bus_TerminalMessage_Keywords WHERE Content IN (select Content from Bus_TerminalMessage_K ...

  6. sql server删除重复记录只保留一条

    今天遇到一个历史导入数据重复的问题,于是要删除重复的记录,一开始想用子查询的方式找到要删除记录的id删除,后来发现DELETE语句可以直接用外连接,这样更加简单,效率也更高. delete sys_p ...

  7. Sql server 删除重复记录的SQL语句

    原文地址:https://www.cnblogs.com/luohoufu/archive/2008/06/05/1214286.html 在项目开发过程中有个模块日清表页面数据重复,当时那个页面的数 ...

  8. MS Sql Server 消除重复行 保留信息完整的一条 2011-11-26 13:19(QQ空间)

    select company ,count(company) as coun into myls from mylist group by company having count(company)& ...

  9. 在MS SQL删除重复行的几种方法

    1.如果有ID字段,就是具有唯一性的字段         delect   table   where   id   not   in   (             select   max(id) ...

随机推荐

  1. svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法

    今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...

  2. iOS面试必看

    转载:http://www.jianshu.com/p/5d2163640e26 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形 ...

  3. 搜索框(Thinkphp5.0)

    1.普通关键词搜索框 模板部分代码: <form name='searchform' action='/index.php/module/controller/search' method='g ...

  4. Summary - SNMP Tutorial

    30.13 Summary Network management protocols allow a manager to monitor and control routers and hosts. ...

  5. [Android]ListView学习笔记

    基本用法流程 创建Adapter并且派生自BaseAdapter,实现其必要的接口方法 将创建的Adapter分配给ListView对象:mPhoneBookListView.setAdapter(p ...

  6. Win7硬盘整数分区一览表

    10G=10245 MB 20G=20482 MB 30G=30726 MB 40G=40963 MB 50G=51208 MB 60G=61444 MB 70G=71681 MB 80G=81926 ...

  7. Nginx 和 IIS 实现动静分离

    前段时间,搞Nginx+IIS的负载均衡,想了解的朋友,可以看这篇文章:<nginx 和 IIS 实现负载均衡>,然后也就顺便研究了Nginx + IIS 实现动静分离.所以,一起总结出来 ...

  8. Rss 订阅:php动态生成xml格式的rss文件

    Rss 简介: 简易信息聚合(也 叫聚合内容)是一种描述和同步网站内容的格式.使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新.网络用户可以在客户端借助于支持RS ...

  9. [译]Writing Custom Middleware in ASP.NET Core 1.0

    原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...

  10. 多材质(Shader)实现

    最近在cocos creator上打算写个U3D中shader功能的插件(能在属性面板调整shader属性). 对其中一个功能有点疑惑,就是U3D中一个渲染物体上可以挂多个材质,后来查询了下,一个物体 ...