Oracle Join two table with timestamp condition with + OR - INTERVAL 30 秒

Oracle Join two table with timestamp condition with + OR - INTERVAL 30 SeC

我有2张桌子
TableA.TimeStamp
TableB.CreateTIME

他们中的大多数时间都相等,有时他们相差不到 60 秒。

我需要创建一个连接并包含条件,如果它们彼此相隔 60 秒就可以了

SELECT TableA.Blah,TableB.Blah
FROM TableA TableA on TableB.CreateTime = TableA.TimeStamp 

使用 ABS 进行日期转换和比较。通常日期数学最适合 INTERVAL 语法,但在这种情况下不是因为 ABS 不适用于间隔。

create table TableA(id number, TimeStamp timestamp);
create table TableB(id number, CreateTIME timestamp);

insert into TableA values (1, timestamp '2000-01-01 00:00:00');
insert into TableA values (2, timestamp '2000-01-01 00:00:30');
insert into TableB values (3, timestamp '2000-01-01 00:00:45');
insert into TableB values (4, timestamp '2000-01-01 00:00:59');

select TableA.id a_id, TableB.id b_id
from TableA
join TableB
  on abs(cast(TableA.TimeStamp as date)-cast(TableB.CreateTIME as date)) < 30/(24*60*60);

A_ID   B_ID
----   ----
2      3
2      4

这里是 SQLFiddle