我有一个带有一串字符串属性和一个时间戳的表。主键由4个字符串属性和时间戳组成。通过解析日志消息来生成行。不幸的是,一个事件创建了几条日志消息,除了时间戳(时间戳相差几秒钟)外,它们的内容相同。因此,现在我有很多行,每个行在每个属性上都相同,但时间戳记和所有时间戳记间隔均为15秒。我想删除所有“重复”行(此处重复,这意味着所有属性都是相同的,除了时间戳,并且时间戳最多相差15秒),除了具有最早时间戳的行。
Postgres版本是9.6。
我能够编写一个查询,向我显示所有这样的“重复”对,但是我想出了一个解决方案,可以删除除最旧的行以外的所有行。在stackoverflow上,我也无法采用任何解决方案来进行重复删除。可能需要带有group by的东西,并从不在其中的互锁中删除*(选择min(timestamp)...):
显示“重复”对的查询:
select *
from interlock i1
inner join interlock i2
on
i1.A = i2.A and
i1.B = i2.B and
i1.C = i2.C and
i1.D = i2.D and
i1.E = i2.E and
i1.F = i2.F and
i1.G = i2.G and
i1.H = i2.H and
i1.I = i2.I and
i1.J = i2.J and
i1.K = i2.K and
i1.timestamp <> i2.timestamp and
abs(extract(epoch from i1.timestamp) - extract(epoch from i2.timestamp)) < 15;
答案 0 :(得分:0)
您可以使用delete
:
delete from interlock i
from interlock i2
where i2.a = i.a and . . .
i2.timestamp > i.timestamp and
i2.timestamp < i.timestamp + interval '15 second';
答案 1 :(得分:0)
这是我最终使用的:
delete from interlock i1
where exists
(select 1 from interlock i2
where i2.room = i1.room
and i2.interlock_device = i1.interlock_device
and i2.interlock_name = i1.interlock_name
and i2.title = i1.title
and i2.information = i1.information
and i2.explanation = i1.explanation
and i2.interlock_id = i1.interlock_id
and i2.error_id = i1.error_id
and i2.severity = i1.severity
and i2.acknowledged = i1.acknowledged
and i2.target = i1.target
and i2.timestamp < i1.timestamp
and i2.timestamp + interval '15 second' > i1.timestamp);