SQL Server中删除重复数据的几个方法
来源:asp之家 发布时间:2009-10-30 10:50:00
数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……
方法一
以下为引用的内容:
declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0
方法二
有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。
1、对于第一种重复,比较容易解决,使用
以下为引用的内容:
select distinct * from tableName
就可以得到无重复记录的结果集。
如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
以下为引用的内容:
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下
假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集
以下为引用的内容:
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)
猜你喜欢
- 本篇博客参考Keqi Zhang的文章“A Progressive Morphological Filter for Removing No
- 下面都是我学习W3C School教程中有关JavaScript写的学习笔记,每个方法我都亲身测试过,每个方法的用途和参数列表,我都是在经过
- 本文为大家分享了python数据分析数据标准化及离散化的具体内容,供大家参考,具体内容如下标准化1、离差标准化是对原始数据的线性变换,使结果
- 以下是服务端代码var HTTPREQUEST_PROXYSETTING_DEFAULT = 0;var&nb
- 1、选取最适用的字段属性MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快。因此,在创建表
- 最简单的关闭一个窗口的方法是点击标题栏上的 x 标志。但是,在下面的例子中,我们将展示如何通过编程来控制关闭窗口。我们将使用PyQt5的信号
- 有一个优秀的库可以使用————demjson示范链接http
- 本文实例为大家分享了python远程连接MySQL数据库的具体代码,供大家参考,具体内容如下连接数据库这里默认大家都已经配置安装好 MySQ
- 很简单的一个东西,在'\n'、'\r\n'、'\r'3中换行符之间进行转换。用法usage:
- 读取文件时报错:xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Exp
- 很多Python学习者想必都会有如下感悟:最开始学习Python的时候,因为没有去探索好用的工具,吃了很多苦头。后来工作中深刻体会到,合理使
- 引言写这篇文章主要是周末休息太无聊,看了看别人代码,发现基本上要么是多协程下载文件要么就只有单协程的断点续传,所以就试了试有进度条的多协程下
- 我们平常在网页上显示的字体最小一般是12PX,当小于10PX时,显示的效果就大打折扣了,因为中文默认的字体是宋体,当小于12PX时的效果如下
- 三通道数组转成彩色图片 img=np.array(img1)img=img.reshape(3,img1.shap
- 但Insus.NET想到的是,从下面的SQL语句,就可以查询到数据库大小。因此分享于此。 代码如下:SELECT DB_NAME
- 1. 概念1.1 基本概念时间,对于我们来说很重要,什么时候做什么?什么时候发生什么?没有时间的概念,生活就乱了。在日常的运维当中,我们更关
- 散点图散点图是指在 回归分析中,数据点在直角坐标系平面上的 分布图,散点图表示因变量随 自变量而 变
- ASP中给函数传参确实是个麻烦事,当参数个数特别多的时候(比如有七八个或者更多的参数个数)差不多就要眼冒金星了,一个个的数吧。而且要命的是参
- 准备正常情况下,创建class的实例后,可以给该实例绑定任何属性和方法,这就是动态语言的灵活性。首先定义一个classclass A(obj
- 一、前言MySQL 的锁按照范围可以分为全局锁、表锁、行锁,其中行锁是由数据库引擎实现的,并不是所有的引擎都提供行锁,MyISAM 就不支持