两个表中的SQL内部联接只匹配一次
我正在努力找到一种有效的方法来自内联接一个有1m行的表 表t的别名为两个表中的SQL内部联接只匹配一次,sql,postgresql,Sql,Postgresql,我正在努力找到一种有效的方法来自内联接一个有1m行的表 表t的别名为t1和t2: Timestamp col1 col2 我想基于timestamp和col1进行内部连接,以便只匹配一次: 如果有多个匹配项,则从右表返回第一个匹配项 并且匹配的列已标记,不能用于下一次匹配 在伪代码中 for v in t1.col1: if t1.timestamp - t2.timestamp < 5 mins AND t2.mark == False AND t1.col1 = t2.col1
t1
和t2
:
Timestamp col1 col2
我想基于timestamp和col1进行内部连接,以便只匹配一次:
for v in t1.col1:
if t1.timestamp - t2.timestamp < 5 mins AND t2.mark == False AND t1.col1 = t2.col1:
t2.mark = true <- don't know how to do this in SQL
return t1.timestamp, t1.col1, t1.col2, t2.timestamp, t2.col2
样本输出数据:
open_time close_time name open_price close_price
2020-11-16 01:07:38.147501 2020-11-16 01:53:17.690113 A 1.0 1.5
为了简化这个问题,请考虑我有以下的记录:
open_time close_time name open_price close_price
2020-11-16 01:07:38.147501,2020-11-16 01:53:17.690113, a, 1
2020-11-16 01:07:38.147501,2020-11-16 01:53:03.217847, a, 1.1
2020-11-16 01:07:40.294957,2020-11-16 01:53:17.690113, a, 1.2
2020-11-16 01:07:40.294957,2020-11-16 01:53:03.217847, a, 1.3
如何使所有记录的打开时间和关闭时间不重叠?我将此问题解释为两个表中存在重复项,并且希望将它们合并,因此只有一个重复的行匹配 可以通过枚举每个表中的行来解决此问题:
select . . .
from (select t1.*,
row_number() over (partition by col, timestamp order by timestamp) as seqnum
from t1
) t1 join
(select t2.*,
row_number() over (partition by col, timestamp order by timestamp) as seqnum
from t2
) t2
on t1.col = t2.col and
t1.timestamp = t2.timestamp and
t1.seqnum = t2.seqnum
请提供样品数据和预期结果。谢谢。更新的样本数据。这有意义吗@戈登林诺夫
select . . .
from (select t1.*,
row_number() over (partition by col, timestamp order by timestamp) as seqnum
from t1
) t1 join
(select t2.*,
row_number() over (partition by col, timestamp order by timestamp) as seqnum
from t2
) t2
on t1.col = t2.col and
t1.timestamp = t2.timestamp and
t1.seqnum = t2.seqnum