我试图从代表交易价格和市场交易量的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分钟的蜡烛,我需要组合五个1分钟蜡烛的数据。
新蜡烛需要遵循以下规则
在这个例子中,我刚收集了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
忽略打开和关闭列时,此操作正常。我无法弄清楚如何在此查询中包含打开和关闭列。我尝试过使用子查询,但没有成功。
答案 0 :(得分:1)
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
在DEMO中,我删除了where time > (UNIX_TIMESTAMP() - 300)