删除重复记录保留最新记录
Deleting duplicate records leaving the latest
在 table 我有 2 列,IP 地址和日期。有 ip 地址重复值。但是日期是独一无二的。我需要删除所有重复的 IP,留下最新的 IP
假设您没有空值并且已分配主键,请尝试此查询...
DELETE FROM MyTable
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, ipaddress, date
FROM MyTable
GROUP BY ipaddress, date
) as KeepRows ON
MyTable.RowId = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL
根据您的要求编辑...
首先您需要将身份设置为您的 table,使用此设置身份
ALTER TABLE [tablename] ADD Id INT IDENTITY(1,1)
设置身份后,执行以下查询以删除重复的 IP 地址..
DELETE FROM dbo.DataTime WHERE ColumnID NOT IN (SELECT MIN(ColumnID) _
FROM dbo.DataTime group by (ipaddress))
你可以试试DELETE JOIN
如果你用mysql
:
delete t1
from yourtable t1
join yourtable t2
on t1.ip = t1.ip
and t1.`date` < t2.`date`
试试这个
delete
from yourtable aa
where
ROWID in
(
Select ROWID
from
yourtable bb
where aa.ip = bb.ip
and aa.date > bb.date
)
您想删除所有存在较新条目(即具有相同 IP 地址和较早日期的条目)的记录:
delete from mytable
where exists
(
select *
from mytable newer
where newer.ipaddress = mytable.ipaddress
and newer.date > mytable.date
);
此查询向您展示了一种为 ip 选择除最新行以外的所有行的方法:
WITH
--this bit of code just creates some data we can play with because I
--don't have your table structure in my database
ip_data (ip, creation_date)
AS
(SELECT '1.2.3.4',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-4 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-5 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-4 FROM dual
)
--this query takes our data with row numbers and excludes any with row number
--1 (that is, the most recent row for each ip)
SELECT
ip
,creation_date
FROM
--this query assigns a row number to each row. The latest row for an ip
--get row number 1. Numbering restarts for each ip (PARTITION BY ip)
(SELECT
ip
,creation_date
,ROW_NUMBER() OVER (PARTITION BY ip ORDER BY creation_date DESC) rn
FROM
ip_data
)
WHERE rn > 1
;
您需要为您的 table 结构修改此内容,但希望评论有意义。一旦你理解了它是如何工作的,你就可以将它放入类似下面的东西中:
DELETE FROM <your table>
WHERE (ip, creation_date) IN
(<select statement similar to the above>)
在 table 我有 2 列,IP 地址和日期。有 ip 地址重复值。但是日期是独一无二的。我需要删除所有重复的 IP,留下最新的 IP
假设您没有空值并且已分配主键,请尝试此查询...
DELETE FROM MyTable
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, ipaddress, date
FROM MyTable
GROUP BY ipaddress, date
) as KeepRows ON
MyTable.RowId = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL
根据您的要求编辑...
首先您需要将身份设置为您的 table,使用此设置身份
ALTER TABLE [tablename] ADD Id INT IDENTITY(1,1)
设置身份后,执行以下查询以删除重复的 IP 地址..
DELETE FROM dbo.DataTime WHERE ColumnID NOT IN (SELECT MIN(ColumnID) _
FROM dbo.DataTime group by (ipaddress))
你可以试试DELETE JOIN
如果你用mysql
:
delete t1
from yourtable t1
join yourtable t2
on t1.ip = t1.ip
and t1.`date` < t2.`date`
试试这个
delete
from yourtable aa
where
ROWID in
(
Select ROWID
from
yourtable bb
where aa.ip = bb.ip
and aa.date > bb.date
)
您想删除所有存在较新条目(即具有相同 IP 地址和较早日期的条目)的记录:
delete from mytable
where exists
(
select *
from mytable newer
where newer.ipaddress = mytable.ipaddress
and newer.date > mytable.date
);
此查询向您展示了一种为 ip 选择除最新行以外的所有行的方法:
WITH
--this bit of code just creates some data we can play with because I
--don't have your table structure in my database
ip_data (ip, creation_date)
AS
(SELECT '1.2.3.4',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-4 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-5 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-4 FROM dual
)
--this query takes our data with row numbers and excludes any with row number
--1 (that is, the most recent row for each ip)
SELECT
ip
,creation_date
FROM
--this query assigns a row number to each row. The latest row for an ip
--get row number 1. Numbering restarts for each ip (PARTITION BY ip)
(SELECT
ip
,creation_date
,ROW_NUMBER() OVER (PARTITION BY ip ORDER BY creation_date DESC) rn
FROM
ip_data
)
WHERE rn > 1
;
您需要为您的 table 结构修改此内容,但希望评论有意义。一旦你理解了它是如何工作的,你就可以将它放入类似下面的东西中:
DELETE FROM <your table>
WHERE (ip, creation_date) IN
(<select statement similar to the above>)