我需要使用{Name,DateTime,Open,High,Low,Close,Volume}将股票市场“ 1min”数据分组为不同的时间范围,即。在MYSQL上为“ 5分钟/ 15分钟/ 60分钟”。架构建立在sqlfiddle - http://sqlfiddle.com/#!2/91433。
我找到了一个链接-使用T-SQL将OHLC-Stockmarket数据分组为多个时间范围,并且对MSSQL的要求类似。 我试图跟随链接- http://briansteffens.com/2011/07/19/row_number-partition-and-over- in-mysql/,让ROW_NUMBER(),在MySQL中的分区来解决这个问题。
我是sql的新手,有人可以指出正确的方向吗?
最后,使用以下mysql查询解决了该问题:
select min(a.mydate),max(a.myhigh) as high,min(a.mylow) as low, min(case when rn_asc = 1 then a.myopen end) as open, min(case when rn_desc = 1 then b.myclose end) as close from( select @i := if((@lastdate) != (Floor(unix_timestamp(mydate)/300 )), 1, @i + 1) as rn_asc, mydate, myhigh, mylow, myopen, myclose, @lastdate := (Floor(unix_timestamp(mydate)/300 )) from onemindata_1, (select @i := 0) vt1, (select @lastdate := null) vt2 order by mydate ) a inner join( select @j := if((@lastdate1) != (Floor(unix_timestamp(mydate)/300 )), 1, @j + 1) as rn_desc, mydate,myclose, @lastdate1 := (Floor(unix_timestamp(mydate)/300 )) from onemindata_1, (select @j := 0) vt1, (select @lastdate1 := null) vt2 order by mydate desc )b on a.mydate=b.mydate group by (Floor(unix_timestamp(a.mydate)/300 ))
最困难的部分是获取“特定时间间隔”的打开和关闭。我正在对’date’进行’high,low,open’和’close’的内部联接。我可以通过更改(Floor(unix_timestamp(mydate)/ 300))中的分母来切换时间间隔。目前不担心性能,只要它可以工作:)。