Group OHLC-Stockmarket Data into multiple timeframes - Mysql

后端 未结 4 1374
死守一世寂寞
死守一世寂寞 2021-01-01 00:52

I need to group stockmarket \"1min\" data with {Name, DateTime, Open, High, Low, Close, Volume} into different timeframes ie. \"5mins/15mins/60mins\" on MYSQL. Schema built

4条回答
  •  庸人自扰
    2021-01-01 01:20

    I know this is an old question, but look at this much "simpler" solution. There is a trick for open and close price. You might like it.

    SELECT
      FLOOR(MIN(`timestamp`)/"+period+")*"+period+" AS timestamp,
      SUM(amount) AS volume,
      SUM(price*amount)/sum(amount) AS wavg_price,
      SUBSTRING_INDEX(MIN(CONCAT(`timestamp`, '_', price)), '_', -1) AS `open`,
      MAX(price) AS high,
      MIN(price) AS low,
      SUBSTRING_INDEX(MAX(CONCAT(`timestamp`, '_', price)), '_', -1) AS `close`
    FROM transactions_history -- this table has 3 columns (timestamp, amount, price)
    GROUP BY FLOOR(`timestamp`/"+period+")
    ORDER BY timestamp  
    

    period is in seconds

提交回复
热议问题