2015-12-15 98 views
3

我仍然在处理与当我询问my previous question on Stack Overflow时相同的项目。我的SQL表格在这个问题中有完整的描述,我会请你阅读这个来理解我的新问题。两个表之间的对应关系的复杂SQL查询

所不同的是现在这两个表交易事件不再同步:现在,时间不完全相同的两个表之间的对应。然而,我知道还有两个表之间一到一个对应关系,这意味着每个贸易有一个相应的事件,但有些事件不对应于贸易

交易

id | time | price | volume | foo 
-----+-----------+---------+--------+------- 
201 | 32400.524 |  53 | 2085 | xxx 
202 | 32400.530 |  53 | 1162 | xxx 
203 | 32400.531 | 52.99 |  50 | xxx 
204 | 32401.532 | 52.91 | 3119 | xxx 
205 | 32402.437 | 52.91 | 3119 | xxx 
206 | 32402.832 | 52.91 | 3119 | xxx 
207 | 32403.255 | 52.91 | 3119 | xxx 
208 | 32404.242 | 52.92 | 3220 | xxx 
209 | 32405.823 | 52.92 | 3220 | xxx 
210 | 32406.839 | 52.92 | 3220 | xxx 

活动

id | time | price | volume | bar 
-----+-----------+---------+--------+------ 
328 | 32399.345 | 52.91 | 3119 | yyy 
329 | 32400.964 | 52.91 | 3119 | yyy 
330 | 32401.194 | 52.91 | 3119 | yyy 
331 | 32401.746 | 52.91 | 3119 | yyy 
332 | 32401.823 | 52.91 | 3119 | yyy 
333 | 32402.534 | 52.91 | 3119 | yyy 
334 | 32402.876 | 52.92 | 3220 | yyy 
335 | 32403.839 | 52.92 | 3220 | yyy 
336 | 32404.634 | 52.92 | 3220 | yyy 
337 | 32405.234 | 52.91 | 2501 | yyy 

我想通过使两个表之间的对应关系最小化交易和事件之间的时间差。这是有道理的:如果有多个事件与交易量和价格相对应,那么我们必须从交易中采取最“最迟”的事件。

我试图做以下的事情:

SELECT 
    t.*, 
    (SELECT e.id 
     FROM events o 
     WHERE e.price = t.price 
     AND e.volume = t.volume 
     ORDER BY ABS(o.time - t.time) 
     LIMIT 1 
    ) as most_probable_corresponding_event_id 
FROM trades t 
ORDER BY t.time; 

但问题是,这个查询不给唯一的对应关系:同一个事件E可以选择不同行业t1和t2,如果这个事件离交易t1和t2最近。我想要的是独家对应。

谢谢你的帮助。

编辑:

的例子DATAS输出我预计会是:

trade_id | order_id | price | volume | bar | foo 
-----------+----------+---------+--------+------+------- 
     204 | 331 | 52.91 | 3119 | xxx | yyy 
     205 | 333 | 52.91 | 3119 | xxx | yyy 
     206 | 334 | 52.91 | 3119 | xxx | yyy 
     207 | 335 | 52.92 | 3220 | xxx | yyy 
     208 | 336 | 52.92 | 3220 | xxx | yyy 
     209 | 337 | 52.92 | 3220 | xxx | yyy 
+0

您能添加您的预期输出吗? – davejal

+0

好吧,让我来做 – Edouardb

+0

我已经添加了示例数据的预期输出。目标仅仅是为了将xxx和yyy数据集中在一张表中而进行对应。 – Edouardb

回答

0

我试着做了很多,但不能得到你的结果。我得到了一些可能有所帮助

使用以下查询,您将获得具有相同价格和交易量以及事件和交易之间的时差的所有记录。

select * from 
(SELECT t.id as trade_id, e.id as event_id, e.price as price, e.volume as volume,e.bar as bar, t.foo as foo, abs(e.time-t.time) as diff 
FROM events e 
inner JOIN trades t on t.price = e.price AND t.volume = e.volume order by trade_id,diff asc) a 

使用你的数据就不可能得到一些你所期望的结果。即206不能使用价格和音量匹配的order_id 334。

我觉得要克服你需要改变你的数据库,并添加一个外键(使它可以做到更容易连接)

以下查询会给你一个结果大部分问题,但EVENT_ID不匹配,无论是因为前面提到的问题(价格和音量匹配),还是因为最接近时间匹配的事件记录不是您实际需要的。

select * from 
(SELECT t.id as trade_id, e.id as event_id, e.price as price, e.volume as volume,e.bar as bar, t.foo as foo, abs(e.time-t.time) as diff FROM events e 
inner JOIN trades t on t.price = e.price AND t.volume = e.volume order by trade_id,diff asc) a group by trade_id 
0

根据您在上一个问题中所写的内容以及示例数据,我预计在事件表中时间总是比交易表中的时间晚。因此,如果添加事件时间>订单时间的条件,那么应该为您提供唯一的匹配,除非事件表落后于交易。

SELECT 
t.*, 
(SELECT e.id 
    FROM events o 
    WHERE e.price = t.price 
    AND e.volume = t.volume 
    AND o.time > t.time 
    ORDER BY ABS(o.time - t.time) 
    LIMIT 1 
) as most_probable_corresponding_event_id 
FROM trades t 
ORDER BY t.time;