我有一个历史外汇历史1分钟级别数据的表格,我想根据1分钟级别的数据选出10分钟的数据。
我的表格如下所示:
ccy Time open high low close
AUDUSD 2018.03.26 00:00 0.77282 0.77283 0.77265 0.77265
AUDUSD 2018.03.26 00:01 0.77264 0.77266 0.77261 0.77266
AUDUSD 2018.03.26 00:02 0.77264 0.77265 0.77259 0.77264
AUDUSD 2018.03.26 00:03 0.77262 0.77262 0.7726 0.77262
AUDUSD 2018.03.26 00:04 0.77262 0.77262 0.77262 0.77262
AUDUSD 2018.03.26 00:05 0.7726 0.7726 0.7726 0.7726
AUDUSD 2018.03.26 00:06 0.77259 0.77262 0.77259 0.77261
AUDUSD 2018.03.26 00:07 0.77262 0.77265 0.77262 0.77264
AUDUSD 2018.03.26 00:08 0.77263 0.77272 0.77262 0.77272
AUDUSD 2018.03.26 00:09 0.77273 0.77278 0.77271 0.77274
AUDUSD 2018.03.26 00:10 0.77273 0.77282 0.77271 0.77279
AUDUSD 2018.03.26 00:11 0.77282 0.77293 0.77281 0.77291
AUDUSD 2018.03.26 00:12 0.77291 0.77293 0.77287 0.77287
AUDUSD 2018.03.26 00:13 0.77288 0.77288 0.77288 0.77288
AUDUSD 2018.03.26 00:14 0.77288 0.77288 0.77277 0.77279
AUDUSD 2018.03.26 00:15 0.77278 0.77279 0.77255 0.77262
AUDUSD 2018.03.26 00:16 0.77261 0.77271 0.77261 0.77271
AUDUSD 2018.03.26 00:17 0.77271 0.77273 0.77264 0.77271
AUDUSD 2018.03.26 00:18 0.77273 0.77282 0.77273 0.77281
AUDUSD 2018.03.26 00:19 0.77281 0.77285 0.77281 0.77283
如您所见,时间戳是每 1 分钟一次,以上是 20 分钟的数据样本。
我期望的结果是:
ccy Time open high low close
AUDUSD 2018.03.26 00:00 0.77282 0.77283 0.77259 0.77274
AUDUSD 2018.03.26 00:10 0.77273 0.77293 0.77255 0.77283
开盘需要是时间跨度的每个起点的开盘价,比如从2018.03.26 00:00到2018.03.26 00:09,起点是2018.03.26 00:00 ,所以预期开盘价为2018.03.26 00:00的开盘价,即0.77282。
收盘价需要是时间跨度每个终点的收盘价,比如从2018.03.26 00:00到2018.03.26 00:09,终点是2018.03.26 00:09 ,所以预期收盘价是2018.03.26 00:09的收盘价,即0.77274。
最高值必须是时间跨度的最大值。在high栏中,从2018.03.26 00:00到2018.03.26 00:09,最大值是0.77283,所以我的预期high是0.77283。
最低值需要是时间跨度的最小值。在low栏中,从2018.03.26 00:00到2018.03.26 00:09,最小值是0.77259,所以我预期的low是0.77259。
我希望这可以在我有更多数据时继续循环。因此,例如,如果我有 1 小时的数据,当我将数据聚合为 10 分钟的数据时,我预计会有 60/10 = 6 行记录。
我已经为此工作了一段时间,但找不到合适的解决方案,请有人帮忙吗?非常感谢!
最佳答案
问题中有很多事情不清楚。这看起来像货币转换数据,但问题中没有说明(ccy?),看起来你希望按 ccy 聚合但问题中没有说明,没有迹象表明你希望传递给查询的内容(区间范围、区间类型等)。但作为开始,这里有一个方法示例,我看到的关键问题是获取第一个和最后一个值
drop table if exists t;
create table t(ccy varchar(6),Tm datetime,open decimal(10,5),high decimal(10,5), low decimal(10,5), close decimal(10,5));
insert into t values
( 'AUDUSD', '2018-03-26 00:00' , 0.77282 , 0.77283 , 0.77265 , 0.77265),
( 'AUDUSD', '2018-03-26 00:01' , 0.77264 , 0.77266 , 0.77261 , 0.77266),
( 'AUDUSD', '2018-03-26 00:02' , 0.77264 , 0.77265 , 0.77259 , 0.77264),
( 'AUDUSD', '2018-03-26 00:03' , 0.77262 , 0.77262 , 0.7726 , 0.77262),
( 'AUDUSD', '2018-03-26 00:04' , 0.77262 , 0.77262 , 0.77262 , 0.77262),
( 'AUDUSD', '2018-03-26 00:05' , 0.7726 , 0.7726 , 0.7726 , 0.7726),
( 'AUDUSD', '2018-03-26 00:06' , 0.77259 , 0.77262 , 0.77259 , 0.77261),
( 'AUDUSD', '2018-03-26 00:07' , 0.77262 , 0.77265 , 0.77262 , 0.77264),
( 'AUDUSD', '2018-03-26 00:08' , 0.77263 , 0.77272 , 0.77262 , 0.77272),
( 'AUDUSD', '2018-03-26 00:09' , 0.77273 , 0.77278 , 0.77271 , 0.77274),
( 'AUDUSD', '2018-03-26 00:10' , 0.77273 , 0.77282 , 0.77271 , 0.77279);
select ccy, (select open from t t1 where t1.ccy = t.ccy order by t1.tm asc limit 1) periodopen,
(select close from t t1 where t1.ccy = t.ccy order by t1.tm desc limit 1) period_close,
max(high) period_high,
min(low) period_low
from t
where t.tm between '2018-03-26 00:00' and '2018-03-26 00:10:00'
group by ccy;
+--------+------------+--------------+-------------+------------+
| ccy | periodopen | period_close | period_high | period_low |
+--------+------------+--------------+-------------+------------+
| AUDUSD | 0.77282 | 0.77279 | 0.77283 | 0.77259 |
+--------+------------+--------------+-------------+------------+
1 row in set (0.00 sec)
关于mysql - 如何使用sql查询将历史外汇数据从1分钟级别的数据聚合到10分钟级别的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056658/