2017-10-05 60 views
0

我有這些列的表:如何編寫刪除重複的帖子SQL腳本

id (pk, int identity), imei (varchar), name (varchar), lastconnected (datetime) 

一些在此表中的條目具有相同的名稱和IMEI,但不同的ID和不同lastconnected日期。

如何有效地過濾掉所有具有重複項的條目(使用SQL腳本),然後刪除最近的日期爲lastconnected的條目?

+0

哪個RDBMS是這樣的?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

+0

當有3個(或更多)具有相同imei和name的行時會發生什麼?你還想刪除其中的一行並保留其他重複項? – SMor

+0

使用SQL服務器 –

回答

1

簡單ROW_NUMBERDELETE應該做的伎倆:

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

它選擇最大的日期名稱和IEMI的每個組合,然後刪除該特定行。

DELETE FROM yourtablee 
WHERE (lastconnecteddate,name,imei) in 
        (SELECT max(lastconnecteddate), name,imei 
        FROM yourtable 
       GROUP BY name,imei) 
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 
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