sql数据重复了。

好帅的一条鱼 2010-01-05 08:47:24

上面的数据有重复的,请问怎么去掉,只保留一条。(同一个人同意日期时间的指保留一条)
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sych888 2010-01-05
  • 打赏
  • 举报
回复
符合什么条件,才算是数据重复
delete from tb t where id! = (select top 1 id from tb where id = t.id and datediff(day,日期时间,t.日期时间) = 0)
dawugui 2010-01-05
  • 打赏
  • 举报
回复
你的cardcode , date , time存在相同的?怎么搞?用另外两个字段?
大致写法为:

select * from tb t where collectlogid = (select max(collectlogid) from tb where cardcode = t.cardcode and date = t.date and time = time)

select * from tb t where not exists (select 1 from tb where cardcode = t.cardcode and date = t.date and time = time and collectlogid > t.collectlogid)
黄_瓜 2010-01-05
  • 打赏
  • 举报
回复
delete tb from tb t where date not exists (select 1 from tb where id = t.id and time=T.time and date  > t.date ) 
SQL77 2010-01-05
  • 打赏
  • 举报
回复
SELECT * FROM TB T WHERE COLLECTLOGLD=
(SELECT MAX(COLLECTLOGLD) FROM TB WHERE
DATE=T.DATE AND TIME=T.TIME AND CARDCODE=T.CARDCODE)
好帅的一条鱼 2010-01-05
  • 打赏
  • 举报
回复
dawugui 2010-01-05
  • 打赏
  • 举报
回复
这里的ID相当于你的人的字段.
大致为:
--search
select * from tb t where 日期时间 = (select max(日期时间) from tb where id = t.id and datediff(day,日期时间,t.日期时间) = 0)

select * from tb t where not exists (select 1 from tb where id = t.id and datediff(day,日期时间,t.日期时间) = 0 and 日期时间 > t.日期时间)

--删除
delete tb from tb t where 日期时间 not in (select max(日期时间) from tb where id = t.id and datediff(day,日期时间,t.日期时间) = 0)

delete tb from tb t where 日期时间 not exists (select 1 from tb where id = t.id and datediff(day,日期时间,t.日期时间) = 0 and 日期时间 > t.日期时间)



黄_瓜 2010-01-05
  • 打赏
  • 举报
回复
看不到图片

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧