我有一个包含这些列的表:
id (pk, int identity), imei (varchar), name (varchar), lastconnected (datetime)
此表中的某些条目具有相同的名称和imei,但不同的ID和不同的最后连接日期。
如何有效过滤掉所有具有重复项的条目(使用SQL脚本),然后删除具有最新lastconnected
日期的条目?
答案 0 :(得分:1)
简单的ROW_NUMBER
和DELETE
应该可以解决问题:
WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY imei, [name] ORDER BY lastconnected DESC)
FROM dbo.YourTable
)
DELETE FROM CTE
WHERE RN = 1;
答案 1 :(得分:1)
这很简单,可以解决您的问题
DECLARE @table TABLE
(
id int,
name varchar(10),
imei varchar(10)
)
insert into @table select 1, 'a','a'
insert into @table select 2, 'b','a'
insert into @table select 3, 'c','a'
insert into @table select 4, 'a','a'
insert into @table select 5, 'c','a'
insert into @table select 6, 'a','a'
insert into @table select 7, 'c','a'
insert into @table select 8, 'a','a'
WHILE (exists (select '' from @table group by name , imei having count(*) > 1))
BEGIN
delete from @table where id in (
select max(id) from @table group by imei , name having count(*) > 1)
End
select * from @table
答案 2 :(得分:1)
我的第一直觉是使用RANK()
。如果事情多次重复,这将删除所有重复项,而不仅仅是最新的重复项。
delete a
from (
select id, imei, name, lastconnected, RANK() over(partition by imei, name order by lastconnected) as [rank] from #temp
) as a
where a.rank>1
答案 3 :(得分:-1)
它为name和iemi的每个组合选择最大日期,然后删除该特定行。
DELETE FROM yourtablee
WHERE (lastconnecteddate,name,imei) in
(SELECT max(lastconnecteddate), name,imei
FROM yourtable
GROUP BY name,imei)