我有與樣本外匯一分鐘欄報價表。spa
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 之間。 這根本是可能的嗎?如何作到這一點。翻譯
我所知道的是,如何選擇最小和最大值兩個日期之間。code
五分鐘的時間間隔是有點疼。 一種方法是將轉換爲秒和除以 300。 而後,得到第一和最後也是棘手。 在這種狀況下,建議技巧使用 substring_index()
和 group_concat()
:get
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);