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) ...
随机推荐
- div+css:div中图片垂直居中
div中图片垂直居中 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- JSPatch 使用
1.JSPatch 准备 地址:https://github.com/bang590/JSPatch 框架:libz.1.tbd , JavaScriptCore.framework 2.cocosp ...
- spring mvc(前置控制器)(转载)
(此文转载:http://www.cnblogs.com/brolanda/p/4265749.html) 一.前置控制器配置与讲解 上篇中理解了IOC容器的初始化时机,并理解了webApplicat ...
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享
string sql = @"select * from T_Excel"; ----------------DataTable Star---------------- ...
- linux命令汇总
GPU使用率 使用率查看,GPU-Util $ nvidia-smi 实时查看 $ watch [options] command 最常用的参数是 -n, 后面指定是每多少秒来执行一次命令. 监视显存 ...
- radio 切换内容
<!DOCTYPE html><html><head> <meta charset=utf-8 /> <title>test</tit ...
- Ubuntu提示卷boot仅剩0字节的硬盘空间,解决办法
查看当前安装的linux内核版本号 dpkg --get-selections |grep linux-image 查看当前使用的内核版本号 uname -a 卸载不需要的内核 sudo apt-ge ...
- php事务
<?php set_time_limit(); function sel($time,$number,$count){ ){ sel($time,$number,$count); return ...
- redis 常用命令
临时启动:redis-server.exe redis.conf/redis-server redis.windows.conf安装Windows服务:redis-server.exe --servi ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...