我有一个表格,可以按时间为每个资源更新条目。为简单起见,我们可以说表中有这些必要的列
-resourceid
-entrytime
由于某些原因,在几毫秒内,桌面上有许多资源元素的条目,我想删除已记录的第二个条目。
我通过在表上进行自联接来查找条目来查找这些条目。
select b.resourceid,b.entrytime
from RESOURCE_DATA_TABLE a
join RESOURCE_DATA_TABLE b on a.resourceid=b.resourceid
where b.collectiontime > a.entrytime
and b.entrytime < (a.entrytime + interval '4 millisecond');
但如何一次性删除它们。
答案 0 :(得分:1)
如果使用窗口函数来查找行,这会更容易:
error: ``a
现在您可以使用它删除行:
select *
from (
select resourceid, entrytime,
entrytime - lag(entrytime) over (partition by resourceid order by entrytime) as diff,
row_number() over (partition by resourceid order by entrytime) as rn
from resource_data_table
) t
where diff < interval '4 millisecond';
答案 1 :(得分:0)
试试这个
Delete
FROM RESOURCE_DATA_TABLE r
WHERE
EXISTS (
select *
from RESOURCE_DATA_TABLE a
join RESOURCE_DATA_TABLE b on a.resourceid=b.resourceid
where b.collectiontime > a.entrytime
and b.entrytime < (a.entrytime + interval '4 millisecond')
and b.resourceid = r.resourceid
and b.entrytime = r.entrytime
)
或者这个
Delete
FROM RESOURCE_DATA_TABLE r
WHERE
EXISTS (
Select * from
(select b.resourceid,b.entrytime
from RESOURCE_DATA_TABLE a
join RESOURCE_DATA_TABLE b on a.resourceid=b.resourceid
where b.collectiontime > a.entrytime
and b.entrytime < (a.entrytime + interval '4 millisecond')
) temp
where temp.resourceid = r.resourceid
and temp.entrytime = r.entrytime
)