Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clickhouse downsample into OHLC time bar intervals

For a table e.g. containing a date, price timeseries with prices every e.g. millisecond, how can this be downsampled into groups of open high low close (ohlc) rows with time interval e.g. minute?

like image 880
toby185 Avatar asked Dec 22 '22 21:12

toby185


1 Answers

While option with arrays will work, the simplest option here is to use use combination of group by timeintervals with min, max, argMin, argMax aggregate functions.

SELECT 
  id,
  minute,
  max(value) AS high,
  min(value) AS low,
  avg(value) AS avg,
  argMin(value, timestamp) AS first,
  argMax(value, timestamp) AS last
FROM security
GROUP BY id, toStartOfMinute(timestamp) AS minute
ORDER BY minute
like image 85
filimonov Avatar answered Dec 28 '22 06:12

filimonov