根据 SQL 中的 30 秒时差连接表

Join tables based on 30 second time difference in SQL

我有 table 笔 buy/sell 笔交易,我将其分为一笔 table 只买入交易和一笔 table 只卖出交易。我想重新加入这两个 tables 并匹配产品相同的买入和卖出,并且买入和卖出之间的执行时间在 10 到 30 秒之间。

我尝试左连接两个 tables 并使用 DATEADD 作为时间增量,但我收到一条错误消息。

理想情况下,我希望新的 table 包含买入 table 和卖出 table 的所有列,但我需要匹配的列是产品和 execution_time 来自两者。

我收到的错误消息:- "invalid data type comparison in predicate"

ON b.product = a.product AND b.execution_time >= DATEADD(second, 30, a.execution_time)

30 秒部分现在比 10 秒更重要,可以是 0-30。

您可以使用 Sybase Datediff 获得想要的结果

MYSQL:-

SELECT B.* 
from tblBuy B INNER JOIN tblSell S
ON B.product = S.product
Where  TIME_TO_SEC(TIMEDIFF(B.execution_time ,S.execution_time )) BETWEEN 10 AND 30;

SYBASE:-

SELECT B.* 
from tblBuy B INNER JOIN tblSell S
ON B.product = S.product
Where  DATEDIFF(SECOND, B.execution_time, S.execution_time) BETWEEN 10 AND 30;