SqlServer 2005中使用row_number()在一个查询中删除重复记录_mssql2005

下面我们来看下,如何利用它来删除一个表中重复记录:

复制代码 代码如下:

If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%')
Drop Table #temp
Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1)
Go
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirsa',23,0)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(4,'John',26,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(5,'Abraham',28,default)
Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(6,'Lincoln',30,default)
Delete T From
(Select Row_Number() Over(Partition By [ID],[Name],[Age],[Sex] order By [ID]) As RowNumber,* From #Temp)T
Where T.RowNumber > 1
Select * From #temp

注意倒数第二句脚本,我们在一个查询实现这个功能.
你可以自己执行T-SQL script 看效果.希望对您开发有帮助!

时间: 2024-08-03 10:10:46

SqlServer 2005中使用row_number()在一个查询中删除重复记录_mssql2005的相关文章

SqlServer2005中使用row_number()在一个查询中删除重复记录的方法_mssql2005

下面我们来看下,如何利用它来删除一个表中重复记录: 复制代码 代码如下: If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%') Drop Table #temp Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1) Go Insert Into #temp ([Id

SQL重复记录查询 查询多个字段、多表查询、删除重复记录的方法_Mysql

SQL重复记录查询 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断  select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 例二:  select * from testtable where numeber in (select number from people group by numb

查询及删除重复记录的SQL语句

查询及删除重复记录的sql语句 (一) 比方说 在a表中存在一个字段"name", 而且不同记录之间的"name"值有可能会相同, 现在就是需要查询出在该表中的各记录之间,"name"值存在重复的项: select name,count(*) from a group by name having count(*) > 1 如果还查性别也相同大则如下: select name,sex,count(*) from a group by nam

查询及删除重复记录

(一) 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete from people where peopleId in (se

mysql中distinct和group by过滤删除重复行

下面先来看看例子:  代码如下 复制代码 table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. 比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录.  代码如下 复制代码 select distinct name from table 得到的结果是: name a b c 好像达到效果了,可是,我想要得到的是id值呢?改一下查询语句吧:  代码如下 复制代码 select d

mysql删除重复记录的sql语句与查询重复记录(1/4)

方法1 delete yourtable where [id] not in ( select max([id]) from yourtable group by (name + value)) 方法2 delete a from 表 a left join( select (id) from 表 group by name,value )b on a.id=b.id where b.id is null 查询及删除重复记录的sql语句 查询及删除重复记录的sql语句 1.查找表中多余的重复记录

SQL 查询和删除重复字段数据的方法_MsSql

例如: id           name         value 1               a                 pp 2               a                 pp 3               b                 iii 4               b                 pp 5               b                 pp 6               c           

oracle查询重复数据和删除重复记录示例分享_oracle

一.查询某个字段重复 复制代码 代码如下:       select *          from User u         where u.user_name in (select u.user_name                                  from User u                                group by u.user_name   having count(*) > 1) 二,删除表中某几个字段的重复 例:表中有条六条

ORACLE查询删除重复记录三种方法_oracle

比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来 复制代码 代码如下: select p1.*   from persons  p1,persons  p2   where p1.id<>p2.id   and  p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address 可以实现上述效果. 几个删除重复记录的SQL语句 1.用rowid方法 2.用g