如何选择行的第一个和最后一个值 之间间隔为5分钟
我有与样本外汇一分钟栏报价表。
id,quote_name,quote_date,quote_time,open_rate,close_rate,high_rate,low_rate
"1417","EURUSD","2015-01-01","13:01:00","1.2096","1.2096","1.2097","1.2096"
"1418","EURUSD","2015-01-01","13:02:00","1.2097","1.2096","1.2097","1.2096"
"1419","EURUSD","2015-01-01","13:04:00","1.2096","1.2098","1.2101","1.2096"
"1420","EURUSD","2015-01-01","13:05:00","1.2099","1.2099","1.2099","1.2099"
有可能创建 select 语句将返回 5 分钟间隔报价。我的意思是它应该选择 5 行每个 5 的格拉斯间隔和返回 open_rate 从第一行中,从上次的报价和 min close_rate 和最大的 high_rate 和 low_rate 之间。 这根本是可能的吗?如何做到这一点。
我所知道的是,如何选择最小和最大值两个日期之间。
解决方法 1:
五分钟的时间间隔是有点疼。 一种方法是将转换为秒和除以 300。 然后,获得第一和最后也是棘手。 在这种情况下,建议技巧使用 substring_index()
和 group_concat()
:
select quote_date, min(open_time) as open_time,
substring_index(group_concat(open_rate order by quote_time), ',', 1) as first_open,
substring_index(group_concat(close_rate order by quote_time desc), ',', 1) as last_close,
min(high_rate), max(high_rate),
min(low_rate), max(low_rate)
from quotes
group by quote_date, floor(to_seconds(quote_time) / 300);
即使只是凡世中一颗小小的尘埃,命运也要由自己主宰,像向日葵般,迎向阳光、勇敢盛开