2016-01-20 63 views
1

我有一個帶有示例外匯的表格,其中包含一分鐘的小節引號。如何選擇行的第一個和最後一個值,並在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,最後一行的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); 
+0

它工作的很好,非常感謝! – WIESIEK

相關問題