• 主页
  • 基础类
  • 应用实例
  • 新技术前沿

删除重复记录(并不完全重复)

好帅的一条鱼 2009-12-31 05:00:53
SELECT distinct EmployeeId from DinnerCollect where Date='2009-06-24' and Time>'16:40'
上面语句出错,“ntext 数据类型不能选为 DISTINCT,因为它不可比。”
SELECT * from DinnerCollect where Date='2009-06-24' and Time>'16:40'
会有一些多余的记录。EmployeeId 这个字段同一个值都会出现两笔(但是两笔有一个字段不一样,是系统自动生成的一个字段,其他字段完全一样,任意删除一笔就可以),需删除一笔。谢谢各位大虾
...全文
83 点赞 收藏 9
写回复
9 条回复
dawugui 2009年12月31日
你再把这个条件加上.
where Date='2009-06-24' and Time>'16:40'

select t.* from DinnerCollect t where where Date='2009-06-24' and Time>'16:40' and Date = (select min(Date) from DinnerCollect where Date='2009-06-24' and Time>'16:40' and  EmployeeId = t.EmployeeId ) 
select t.* from DinnerCollect t where Date='2009-06-24' and Time>'16:40' and not exists (select 1 from DinnerCollect where Date='2009-06-24' and Time>'16:40' and EmployeeId = t.EmployeeId and date < t.date)

select t.* from DinnerCollect t where Date='2009-06-24' and Time>'16:40' and Date = (select max(Date) from DinnerCollect where Date='2009-06-24' and Time>'16:40' and EmployeeId = t.EmployeeId)
select t.* from DinnerCollect t where Date='2009-06-24' and Time>'16:40' and not exists (select 1 from DinnerCollect where Date='2009-06-24' and Time>'16:40' and EmployeeId = t.EmployeeId and date > t.date)
回复 点赞
dawugui 2009年12月31日
[Quote=引用楼主 good212164 的回复:]
SELECT distinct EmployeeId from DinnerCollect where Date='2009-06-24' and Time>'16:40'
上面语句出错,“ntext 数据类型不能选为 DISTINCT,因为它不可比。”
SELECT * from DinnerCollect where Date='2009-06-24' and Time>'16:40'
会有一些多余的记录。EmployeeId 这个字段同一个值都会出现两笔(但是两笔有一个字段不一样,是系统自动生成的一个字段,其他字段完全一样,任意删除一笔就可以),需删除一笔。谢谢各位大虾
[/Quote]

select t.* from DinnerCollect t where Date = (select min(Date) from DinnerCollect where EmployeeId = t.EmployeeId)
select t.* from DinnerCollect t where not exists (select 1 from DinnerCollect where EmployeeId = t.EmployeeId and date < t.date)

select t.* from DinnerCollect t where Date = (select max(Date) from DinnerCollect where EmployeeId = t.EmployeeId)
select t.* from DinnerCollect t where not exists (select 1 from DinnerCollect where EmployeeId = t.EmployeeId and date > t.date)
回复 点赞
ACMAIN_CHM 2009年12月31日
delete from DinnerCollect a
where exists (select 1 from DinnerCollect where EmployeeId=a.EmployeeId and Date>a.Date)
回复 点赞
antony1029 2009年12月31日
[Quote=引用 5 楼 sql77 的回复:]
SQL codedelete afrom #T awhereexists(select1from #Twhere Name=a.Nameand ID<a.ID)
[/Quote]
这个最简单了
回复 点赞
SQL77 2009年12月31日
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)
回复 点赞
pt1314917 2009年12月31日

SELECT * from DinnerCollect a where Date='2009-06-24' and Time>'16:40'
and not exists(select 1 from dinnercollect where EmployeeId =a.EmployeeId and 主键>a.主键)
回复 点赞
--小F-- 2009年12月31日
--2、删除重复记录有大小关系时,保留大或小其中一个记录


--> --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go

--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
方法1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)

方法2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null

方法3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)

方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select min(ID)from #T group by Name)

方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0

方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)

方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)



select * from #T

生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1

(2 行受影响)
*/


--II、Name相同ID保留最大的一条记录:

方法1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)

方法2:
delete a from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null

方法3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)

方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)

方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0

方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID desc)

方法7:
delete a from #T a where ID<any(select ID from #T where Name=a.Name)


select * from #T
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2

(2 行受影响)
*/

回复 点赞
好帅的一条鱼 2009年12月31日
sf快回答
回复 点赞
薪水 2009年12月31日
sf
回复 点赞
发动态
发帖子
MS-SQL Server
创建于2007-09-28

1.4w+

社区成员

25.3w+

社区内容

MS-SQL Server相关内容讨论专区
社区公告
暂无公告