2016-05-18 20 views
1

我有2个表格,订单和费率。我想加入这两个表,并选择的OpenTime和closetime之间的最大值和最小值Mysql加入2表并选择日期范围内的最大值和最小值

表1:订单

id type pair lot  opentime  openprice closeprice closetime 

1 buy  eurusd 0.01 2016-05-02 02:04:07  1.15112  1.14778  2016-05-02 03:05:00 

2 sell gbpusd 0.01 2016-05-02 02:24:17  1.45221  1.44989  2016-05-02 03:05:00 

表2:利率

id pair time   price 

10 eurusd 2016-05-02 03:00:00  1.14522 

9 gbpusd 2016-05-02 03:00:00  1.44726 

8 eurusd 2016-05-02 02:30:00  1.15258 

7 gbpusd 2016-05-02 02:30:00  1.45311 

6 eurusd 2016-05-02 02:00:00  1.15051 

5 gbpusd 2016-05-02 02:00:00  1.45173 

4 eurusd 2016-05-01 01:30:00  1.14258 

3 gbpusd 2016-05-02 01:30:00  1.44326 

2 eurusd 2016-05-02 01:00:00  1.15751 

1 gbpusd 2016-05-02 01:00:00  1.45911 

预期结果

id type pair lot  opentime  openprice closeprice closetime  high timehigh  low  timelow 

1 buy eurusd 0.01 2016-05-02 02:04:07 1.15112 1.14778 2016-05-02 03:05:00 1.15258 2016-05-02 02:30:00 1.14522 2016-05-02 03:00:00 

2 sell gbpusd 0.01 2016-05-02 02:24:17 1.45221 1.44989 2016-05-02 03:05:00 1.45311 2016-05-02 02:30:00 1.44726 2016-05-02 03:00:00 

我试试这个查询,但得到空的结果

没有where子句得到结果不是空
SELECT id,type,pair,lot,opentime,openprice,closeprice,closetime,high,timehigh,low,timelow FROM (SELECT id,type,pair,lot,opentime,openprice,closeprice,closetime FROM `order` ORDER BY closetime DESC) table1 
JOIN (SELECT MAX(price) as high,time as timehigh,pair as pairhigh FROM `rates` GROUP BY pair) table2 ON table1.pair=table2.pairhigh 
JOIN (SELECT MIN(price) as low,time as timelow,pair as pairlow FROM `rates` GROUP BY pair) table3 ON table1.pair=table3.pairlow 
WHERE table2.timehigh between table1.opentime and table1.closetime AND table3.timelow between table1.opentime and table1.closetime 

尝试查询,但预计不会

SELECT id,type,pair,lot,opentime,openprice,closeprice,closetime,high,timehigh,low,timelow FROM (SELECT id,type,pair,lot,opentime,openprice,closeprice,closetime FROM `order` ORDER BY closetime DESC) table1 
JOIN (SELECT MAX(price) as high,time as timehigh,pair as pairhigh FROM `rates` GROUP BY pair) table2 ON table1.pair=table2.pairhigh 
JOIN (SELECT MIN(price) as low,time as timelow,pair as pairlow FROM `rates` GROUP BY pair) table3 ON table1.pair=table3.pairlow 

结果

id type pair lot  opentime  openprice closeprice closetime  high timehigh  low  timelow 

1 buy eurusd 0.01 2016-05-02 02:14:07 1.15112 1.14778 2016-05-02 03:05:00 1.15751 2016-05-02 02:00:00 1.14258 2016-05-02 02:00:00 

2 sell gbpusd 0.01 2016-05-02 03:24:17 1.45221 1.44989 2016-05-02 03:05:00 1.45911 2016-05-02 02:00:00 1.44326 2016-05-02 02:00:00 

如何解决这个问题?

+0

谢谢@ inian – pete

+0

是否有一个sqlfiddle? – Strawberry

+0

没有sqlfiddle @strawberry – pete

回答

3

我相信这现在符合您的要求?

SELECT * 
FROM Orders 
JOIN (SELECT price as maxPrice, pair, tr_time as maxTime FROM Rates 
    JOIN (SELECT Rates.pair, MAX(Rates.price) AS price 
     FROM Rates, Orders 
     WHERE (Rates.tr_time between Orders.opentime and Orders.closetime) 
     GROUP BY Rates.pair) 
    as MaxPrices USING (price, pair)) maxRates USING (pair) 
JOIN (SELECT price AS minPrice, pair, tr_time as minTime FROM Rates 
     JOIN (SELECT Rates.pair, MIN(Rates.price) AS price 
      FROM Rates, Orders 
      WHERE (Rates.tr_time between Orders.opentime and Orders.closetime) 
      GROUP BY Rates.pair) 
    as minPrices USING (price, pair)) minRates USING (pair); 

你的代码结构是太可怜了,我要真正找出到底是怎么回事,但本质上看来你是不知道,总运营商,如MAX()只返回一个值。

你还不清楚是什么你想要的最低/最高价格(我以为每pair它已经)

有代码的运行,看看它是否返回正确的?如果不告诉我它不匹配,我可以开始修补它!

编辑 新的结果:

enter image description here

+0

我相信我已经纠正。我也确信你的预期结果有错误 - 英镑 - >美元,1.44726 @ 0300不是开盘时间之间的最低点,1.45311 @ 0230更低? – davidhood2

+0

@Strawberry我再次编辑过。现在变化选择最低,然后加入以获得正确的(确定的)时间,然后将其加入显示开始/关闭时间的开销表 – davidhood2

+0

约定 - 因为价格不是关键。但是,如果你交叉连接,然后使用where语句来坚持​​在价格和pair字段中的平等,这将工作... – davidhood2

0

试试这个,它输出作为您预期的结果,可能会为你工作;)

SQL Fiddle

的MySQL 5.6架构

CREATE TABLE orders 
    (`id` int, `type` varchar(4), `pair` varchar(6), `lot` float, `opentime` datetime, `openprice` float, `closeprice` float, `closetime` datetime) 
; 

INSERT INTO orders 
    (`id`, `type`, `pair`, `lot`, `opentime`, `openprice`, `closeprice`, `closetime`) 
VALUES 
    (1, 'buy', 'eurusd', 0.01, '2016-05-02 02:04:07', 1.15112, 1.14778, '2016-05-02 03:05:00'), 
    (2, 'sell', 'gbpusd', 0.01, '2016-05-02 02:24:17', 1.45221, 1.44989, '2016-05-02 03:05:00') 
; 


CREATE TABLE rates 
    (`id` int, `pair` varchar(6), `time` datetime, `price` float) 
; 

INSERT INTO rates 
    (`id`, `pair`, `time`, `price`) 
VALUES 
    (10, 'eurusd', '2016-05-02 03:00:00', 1.14522), 
    (9, 'gbpusd', '2016-05-02 03:00:00', 1.44726), 
    (8, 'eurusd', '2016-05-02 02:30:00', 1.15258), 
    (7, 'gbpusd', '2016-05-02 02:30:00', 1.45311), 
    (6, 'eurusd', '2016-05-02 02:00:00', 1.15051), 
    (5, 'gbpusd', '2016-05-02 02:00:00', 1.45173), 
    (4, 'eurusd', '2016-05-01 01:30:00', 1.14258), 
    (3, 'gbpusd', '2016-05-02 01:30:00', 1.44326), 
    (2, 'eurusd', '2016-05-02 01:00:00', 1.15751), 
    (1, 'gbpusd', '2016-05-02 01:00:00', 1.45911) 
; 

查询1

SELECT DISTINCT 
orders.*, mx.high, R1.time AS timehigh, mn.low, R2.time AS timelow 
FROM orders 
LEFT JOIN (
    SELECT orders.pair, rates.time, max(rates.price) AS high 
    FROM orders 
    JOIN rates ON orders.pair = rates.pair AND rates.time <= orders.closetime AND rates.time >= orders.opentime 
    group by orders.pair 
) mx ON mx.pair = orders.pair 
LEFT JOIN (
    SELECT orders.pair, min(rates.price) AS low, rates.time 
    FROM orders 
    JOIN rates ON orders.pair = rates.pair AND rates.time <= orders.closetime AND rates.time >= orders.opentime 
    GROUP BY orders.pair 
) mn ON mn.pair = orders.pair 
LEFT JOIN rates R1 ON mx.pair = R1.pair and R1.price = mx.high 
LEFT JOIN rates R2 ON mn.pair = R2.pair and R2.price = mn.low 

Results

| id | type | pair | lot |    opentime | openprice | closeprice |    closetime | high |     time |  low |     time | 
|----|------|--------|------|-----------------------|-----------|------------|-----------------------|---------|-----------------------|---------|-----------------------| 
| 1 | buy | eurusd | 0.01 | May, 02 2016 02:04:07 | 1.15112 | 1.14778 | May, 02 2016 03:05:00 | 1.15258 | May, 02 2016 02:30:00 | 1.14522 | May, 02 2016 03:00:00 | 
| 2 | sell | gbpusd | 0.01 | May, 02 2016 02:24:17 | 1.45221 | 1.44989 | May, 02 2016 03:05:00 | 1.45311 | May, 02 2016 02:30:00 | 1.44726 | May, 02 2016 03:00:00 | 
+0

1:30不是最低?这是行不通的。合计结果中的非分组列是不确定的。考虑到努力,downvote是苛刻的,但这种解决方案是错误的。 – Strawberry

+0

作为问题的描述,'rates.time'在'orders.opentime'和'orders.closetime'之间,不是吗? – Blank

+0

啊,也许是。尽管如此,这仍然会产生一个不确定的结果。 – Strawberry

0

我觉得davidhood2的钉,但以防万一...

SELECT a.* 
    , b.price high 
    , b.time timehigh 
    , c.price low 
    , c.time timelow 
    FROM orders a 
    JOIN 
    (SELECT x.pair 
      , x.price 
      , x.time 
     FROM rates x 
     JOIN 
      (SELECT r.pair 
        , MAX(r.price) price 
       FROM rates r 
       JOIN orders o 
        ON o.pair = r.pair 
       AND r.time BETWEEN o.opentime AND o.closetime 
       GROUP 
        BY pair 
      ) y 
      ON y.pair = x.pair 
      AND y.price = x.price 
    ) b 
    ON b.pair = a.pair 
    JOIN 
    (SELECT x.pair 
      , x.price 
      , x.time 
     FROM rates x 
     JOIN 
      (SELECT r.pair 
        , MIN(r.price) price 
       FROM rates r 
       JOIN orders o 
        ON o.pair = r.pair 
       AND r.time BETWEEN o.opentime AND o.closetime 
       GROUP 
        BY pair 
      ) y 
      ON y.pair = x.pair 
      AND y.price = x.price 
    ) c 
    ON c.pair = a.pair; 

+------+------+--------+------+---------------------+-----------+------------+---------------------+---------+---------------------+---------+---------------------+ 
| id | type | pair | lot | opentime   | openprice | closeprice | closetime   | high | timehigh   | low  | timelow    | 
+------+------+--------+------+---------------------+-----------+------------+---------------------+---------+---------------------+---------+---------------------+ 
| 1 | buy | eurusd | 0.01 | 2016-05-02 02:04:07 | 1.15112 | 1.14778 | 2016-05-02 03:05:00 | 1.15258 | 2016-05-02 02:30:00 | 1.14522 | 2016-05-02 03:00:00 | 
| 2 | sell | gbpusd | 0.01 | 2016-05-02 02:24:17 | 1.45221 | 1.44989 | 2016-05-02 03:05:00 | 1.45311 | 2016-05-02 02:30:00 | 1.44726 | 2016-05-02 03:00:00 | 
+------+------+--------+------+---------------------+-----------+------------+---------------------+---------+---------------------+---------+---------------------+ 
+0

哇,我不知道你的答案和我的本质区别是什么......我只是把'左连接率'从子查询中拿出来,多么感恩的downvote !!! – Blank

+0

@Reno - 我已经解释了本质的区别。这不是左连接。这是你结果的不确定性。 – Strawberry

相关问题