我有一张名为 TEST
的表格------------------------------------------
ID name intime
------------------------------------------
1 ABC 2015-09-03 10:00:00
1 ABC 2015-09-03 10:00:00
2 XYZ 2015-09-03 11:00:00
2 XYZ 2015-09-03 11:00:00
我的目标是从表中删除重复记录,就像ABC intime重复一样。我只有一个例子,我有多个记录而不仅仅是4个记录。
我正在尝试这样的事情
with cte as
(
select id,InTime ,count(*) as c
from intime as m
where InTime between convert(varchar(10),'2015-09-01 00:01:00',103) and convert(varchar(10),'2015-09-30 00:01:00',103)
group by id,InTime
having count(*) > 1
)
select * from cte
order by ID
输出
id InTime c
----------------------------------
1 2015-09-03 10:00:00 2
2 2015-09-03 11:00:00 2
此查询将为我提供在同一日期具有多个intime的记录
with cte as
(
select ID, inTime,
row_number() over (partition by convert(varchar(10),intime,103) order by intime desc) as r
from intime
where ID in (1)
and
inTime between '2015-09-01 00:01:00' and '2015-09-30 23:59:59'
)
select * from cte
delete from cte
where r > 1
这是将删除重复条目的查询。
我面临的问题是我必须逐个手动输入ID,如果我在(1,2)
中执行ID它输出为:
ID inTime r
2 2015-09-03 11:00:00 1
2 2015-09-03 11:00:00 2
1 2015-09-03 10:00:00 3
1 2015-09-03 10:00:00 4
然后,如果我删除r> 1,它将删除3行。
我想创建一些可以删除表中所有重复记录的内容。我正在使用sql-2008。任何建议都会有所帮助。
答案 0 :(得分:1)
试试这个
with abc as
(
select * ,rn=ROW_NUMBER()over(partition by ID ,name,intime order by columnName)from yourtablename
)
delete from abc where rn>1
答案 1 :(得分:0)
为什么不将数据复制到带有DISTINCT的新表中?
- 复制...... 从yourtablename
中选择DISTINCT *到tmp_yourtablename- 删除原始数据 truncate table yourtablename
- 重装他们...... 从tmp_yourtablename
插入yourtablename select *- 完成所有操作后删除tmp_yourtablename ...