使用mysql 5分钟吧

时间:2011-08-04 10:36:48

标签: mysql datetime

我已经按照以下格式收集了tick数据

"instrument","tick_time","ask","bid","askVol","bidVol"
"EUR/USD","2011-07-24 20:00:01","1.43676","1.43666","1.13","4.13"
"EUR/USD","2011-07-24 20:00:02","1.43676","1.43666","1.13","4.5"
"EUR/USD","2011-07-24 20:00:03","1.43674","1.43664","1.13","1.65"
"EUR/USD","2011-07-24 20:00:06","1.43675","1.43665","1.13","5.4"
"EUR/USD","2011-07-24 20:00:06","1.43677","1.43668","1.13","4.28"
"EUR/USD","2011-07-24 20:00:10","1.43676","1.43666","1.13","6.15"
"EUR/USD","2011-07-24 20:00:11","1.43679","1.43669","1.13","4.13"

现在我正在尝试创建5分钟的栏。我尝试了以下sql。

select    ROUND(tick_time / (60*5)) * 60 * 5 as bar_time,
          max(bid)  as high,
          min(bid) as low
from ticks
group by bar_time

我得到以下数据

"bar_time","high","low"
"20110724199900","1.43669","1.43661"
"20110724200200","1.4366","1.4358"
"20110724200500","1.43625","1.43579"
"20110724200800","1.4365","1.43608"
"20110724201100","1.43645","1.43601"
"20110724201400","1.43624","1.43573"
"20110724201700","1.43619","1.43591"
"20110724202000","1.43644","1.4359"

我如何做以下

  1. 以日期时间格式格式化bar_time?
  2. 将第一个和最后一个数据汇总为列。

3 个答案:

答案 0 :(得分:2)

其他解决方案:

select 
    date_format( 
        date_sub(
            tick_time, 
            INTERVAL (extract(MINUTE from tick_time) % 5) MINUTE
        ), 
        "%m-%d%Y %H:%i"
    ) as  bar_time, 
    MAX(bid) AS high,
    MIN(bid) AS low
from ticks
group by bar_time

答案 1 :(得分:1)

1-以datetime格式格式化bar_time:

假设您的time format%Y%m%d%H%i%s,您可以使用str_to_date mysql函数:

select  str_to_date((ROUND(tick_time / (60*5)) * 60 * 5) , '%Y%m%d%H%i%s') as bar_time,
        max(bid)  as high,
        min(bid) as low
from ticks
group by bar_time

您可能需要将函数的第一个参数(即ROUND(...))转换为字符串。 此外,您的示例中的第一行包含时间“20110724199900”,您确定这99分钟吗?

2-不明白,你能解释一下你的意思吗?

答案 2 :(得分:1)

SELECT    
  STR_TO_DATE
  (
    CONCAT(
      LEFT(ROUND(tick_time / (60*5)) * 60 * 5,10)
      ,MID(ROUND(tick_time / (60*5)) * 60 * 5,10,2)/100*60
      ,RIGHT(ROUND(tick_time / (60*5)) * 60 * 5,2)/100*60
    )
    ,'%Y%m%d%H%i%s'
  ) AS bar_datetime
  , MAX(bid) AS high
  , MIN(bid) AS low
  , CONCAT(MAX(bid), ',', MIN(bid)) as max_min
FROM ticks
GROUP BY bar_datetime