SQL Server删除重复行的6个方法
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个方法的更多相关文章
- SQL查找删除重复行
本文讲述如何查找数据库里重复的行.这是初学者十分普遍遇到的问题.方法也很简单.这个问题还可以有其他演变,例如,如何查找“两字段重复的行”(#mysql IRC 频道问到的问题) 如何查找重复行 第一步 ...
- SQL Server删除表信息的三种方法
1.使用DELETE实现SQL Server删除表信息 (1)删除表中的全部信息 USE student GO DELETE student --不加where条件,删除表中的所有记录 go ...
- SQL Server 删除重复记录,只保留一条记录
原文地址:http://blog.csdn.net/eriato/article/details/17417303 有张表格之前没有设计关键字段的唯一约束,导致有时候执行插入操作时不小心执行了多次就出 ...
- SQL SERVER 合并重复行,行列转换
引用自:http://www.cnblogs.com/love-summer/archive/2012/03/27/2419778.html sql server2000 里面如何实现oracle10 ...
- SQL Server 删除重复数据只保留一条
DELETE FROM Bus_TerminalMessage_Keywords WHERE Content IN (select Content from Bus_TerminalMessage_K ...
- sql server删除重复记录只保留一条
今天遇到一个历史导入数据重复的问题,于是要删除重复的记录,一开始想用子查询的方式找到要删除记录的id删除,后来发现DELETE语句可以直接用外连接,这样更加简单,效率也更高. delete sys_p ...
- Sql server 删除重复记录的SQL语句
原文地址:https://www.cnblogs.com/luohoufu/archive/2008/06/05/1214286.html 在项目开发过程中有个模块日清表页面数据重复,当时那个页面的数 ...
- 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)& ...
- 在MS SQL删除重复行的几种方法
1.如果有ID字段,就是具有唯一性的字段 delect table where id not in ( select max(id) ...
随机推荐
- OVGap iOS与Javascript交互(H5与原生APP交互)
源代码:https://github.com/windshg/OVGap OVGap:一个轻量级的类库,能够让iOS应用和远程网页的 Javascript 代码进行通信,也就是说,远程的 Javasc ...
- 命名空间jquery
命名空间的意思就是 同一个元素有绑定了2个相同的事件,比如2个scroll,这个时候你做其他交互的时候只想触发第二个scroll事件 就可以用命名空间做了 <button id="b ...
- JQuery常用函数及功能小结
1.文档加载完成执行函数$(document).ready(function(){ alert("开始了");});2.添加/删除CSS类$("#some-id&quo ...
- mysql 常用函数整理
1.length(字段名) 2.UNIX_TIMESTAMP(字段名) 3.FROM_UNIXTIME( 1249488000, '%Y%m%d' ) 4.ceil() 5.floor() 6.CAS ...
- Oracle to_date()函数的用法
Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...
- 使用MyEclipse 9.0 创建 struts2 的HelloWorld 工程
作为眼高手低的典范,必须痛改前非,好好动手多做开发! 本文是struts2的入门指导,权当笔记! 在MyEclipse中,配置Apache tomcat 7.x 服务器 创建新的Web project ...
- Json在PHP与JS之间传输
1. JS-->PHP a). JS create Json <script> $(document).ready(function(){ /*--JS create Json--* ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- 关于 FPGA 内部信号扇入扇出
扇入.扇出系数 扇入系数是指门电路允许的输入端数目.一般门电路的扇入系数为1—5,最多不超过8.扇出系数是指一个门的输出端所驱动同类型门的个数,或称负载能力.一般门电路的扇出系数为8,驱动器的扇出系数 ...
- Mac Pro 编译安装 Redis 的 PHP 客户端 phpredis
1.去官网下载 redis 扩展源码包 https://github.com/phpredis/phpredis 2.安装 redis 扩展 /usr/local/src/mac-sdk/source ...