与其他问题相似,但不同......
我有多条记录,如果它们包含与其他记录(未标记为删除)的任何重叠,则标记可能被删除的记录。我希望能够在500万行表上有效地完成这项工作。
这是我的......而且它没有工作(没有找到我手动测试的重复项目)
delete from MYTABLE t1
where t1.DELETABLEFLAG = 'Y' and
exists
(select 1
from
MYTABLE t2
where
t2.MATCHCOLUMN = t1.MATCHCOLUMN
and t2.DELETABLEFLAG <>'Y'
and
((t1.START_DATE between t2.START_DATE and t2.END_DATE) or
(t1.END_DATE between t2.START_DATE and t2.END_DATE) or
(t2.START_DATE between t1.START_DATE and t1.END_DATE) or
(t2.END_DATE between t1.START_DATE and t1.END_DATE) )
)
PS,我显然已将这些列重命名,所以请不要对我的命名发表评论:)
有更有效的方法吗?
PPS。 START_DATE和END_DATE是TIMESTAMPS
答案 0 :(得分:0)
如果满足以下两个条件,则两个时间段重叠:
我会将查询写成:
delete from MYTABLE t1
where t1.DELETABLEFLAG = 'Y' and
exists (select 1
from MYTABLE t2
where t2.MATCHCOLUMN = t1.MATCHCOLUMN AND
t2.DELETABLEFLAG <> 'Y' AND
t1.START_DATE <= t2.END_DATE AND
t1.END_DATE >= t2.START_DATE
);
也许这会有所帮助。