gpt4 book ai didi

mysql选择查询烛台市场数据

转载 作者:行者123 更新时间:2023-11-29 02:42:02 27 4
gpt4 key购买 nike

我正在尝试从代表交易价格和市场交易量的 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 分钟蜡烛图的数据。

新蜡烛需要遵循以下规则

  • 打开的柱子来自 first(candles[open])
  • high 列来自 max(candles[high])
  • 最低柱来自 min(candles[low])
  • close column 来自 last(candles[close])
  • volume 列来自 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

当忽略打开和关闭列时,这可以正常工作。我不知道如何在此查询中包含打开和关闭列。我曾尝试使用子查询,但没有成功。

最佳答案

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)

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

关于mysql选择查询烛台市场数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com