I have a postgreSQL database with the following columns:
timestamp <bigint> - Unix timestamp in milliseconds
open <float> - The first traded price
high <float> - The highest traded price
low <float> - THe lowest traded price
close <float> - The last traded price
volume <float> - The total volumed transacted during the time period
Sometimes I'll query data that spans 30+ days (43.2k rows). These queries take a really long time, so I thought whenever I have >30 days of data I'll fetch 5 minute candles instead which would cut down the row count by 5x.
Basically I'm looking for a function that does this (pseudocode):
SELECT
first_value(timestamp),
first_value(open),
max(high),
min(low),
last_value(close),
sum(volume)
WHERE timestamp > {some timestamp in the past} AND timestamp < {current time}
GROUP BY floor((timestamp / 1000) / (60*5)) * 5
ORDER BY timestamp
Any help would be greatly appreciated, been stuck on this one for a while