2

我正在尝试从代表交易价格和市场量的 mysql 表中选择蜡烛数据的摘要。

该表包含 1 分钟蜡烛数据的数据。我想得到一个 5 分钟的总结(基本上将其解释为 5 分钟的蜡烛)。

该表如下所示:

+--------+------+------+------+-------+--------+-------+
| market | open | high | low  | close | volume | time  |
+--------+------+------+------+-------+--------+-------+
| ABC    | 13.6 | 15.4 | 11.2 | 14.1  | 299.4  | 11:59 |
| ABC    | 14.1 | 16.8 | 12.5 | 15.3  | 342.2  | 12:00 |
| ABC    | 15.3 | 16.3 | 11.2 | 14.2  | 351.5  | 12:01 |
| ABC    | 14.2 | 15.5 | 12.3 | 13.2  | 374.2  | 12:02 |
| DEF    | 93.7 | 94.3 | 90.7 | 93.2  | 1123.3 | 11:59 |
| DEF    | 93.2 | 96.5 | 91.1 | 96.3  | 1232.8 | 12:00 |
| DEF    | 96.3 | 98.2 | 95.3 | 95.4  | 1390.4 | 12:01 |
| DEF    | 95.4 | 97.6 | 93.7 | 94.3  | 1360.9 | 12:02 |
+--------+------+------+------+-------+--------+-------+

我需要从此表中进行选择以生成代表更大时间范围的新蜡烛。例如,要表示 5 分钟蜡烛,我需要组合来自 5 个 1 分钟蜡烛的数据。

新蜡烛需要遵循以下规则

  • 开放列来自第一个(蜡烛[开放])
  • 高列来自 max(candles[high])
  • 低列来自 min(candles[low])
  • 关闭列来自最后一个(蜡烛[关闭])
  • 音量列来自 sum(candles[volume])

在这个例子中,我刚刚收集了 3 支最新的蜡烛

+--------+------+------+------+-------+--------+-------+
| market | open | high | low  | close | volume | time  |
+--------+------+------+------+-------+--------+-------+
| ABC    | 14.1 | 16.8 | 11.2 | 13.2  | 1067.9 | 12:00 |
| DEF    | 93.2 | 98.2 | 91.1 | 94.3  | 3984.1 | 12:00 |
+--------+------+------+------+-------+--------+-------+

到目前为止我有

SELECT
    market,
    MAX(high) AS 'high', 
    MIN(low) AS 'low', 
    SUM(volume) AS 'volume'
FROM
    candles
WHERE
    time > (UNIX_TIMESTAMP() - 300) 
GROUP BY
    market

忽略打开和关闭列时,这可以正常工作。我无法弄清楚如何在此查询中包含打开和关闭列。我尝试过使用子查询,但没有成功。

4

1 回答 1

3
SELECT T2.MARKET,t1.open,T2.High,T2.low,T2.Volume,T3.close,t1.time
FROM 
(SELECT market,open,time FROM candles t1 WHERE TIME IN 
 (SELECT MIN(TIME) FROM Candles WHERE time > (UNIX_TIMESTAMP() - 300) GROUP BY market))
 as T1
Inner Join
(
SELECT
    market,
    MAX(high) AS 'high', 
    MIN(low) AS 'low', 
    SUM(volume) AS 'volume'
FROM
    candles
  WHERE
    time > (UNIX_TIMESTAMP() - 300)
GROUP BY
    market
) AS t2
on t1.market=t2.market
INNER JOIN (SELECT market,Close,Time FROM candles t1 WHERE TIME IN 
 (SELECT MAX(TIME) FROM Candles GROUP BY market)) as t3
 on t2.market=t3.market

在演示中我删除了where time > (UNIX_TIMESTAMP() - 300)

http://sqlfiddle.com/#!9/8f090/7

于 2018-03-31T04:08:12.200 回答