Hello, everyone. I am researching QuestDB for my company, focusing on handling various query scenarios and storing K-line data. I encountered a problem and would like to ask for help on how to implement it:
The problem is: Within a specific time window, if no trades occur, a K-line should still be generated with the open, high, low, and close prices being the last trade price before that time window. For example, my table new_trades
:
CREATE TABLE new_trades (
symbol symbol,
side symbol,
mid long,
tradeId long,
amount_unit double,
price DOUBLE,
amount DOUBLE,
ts TIMESTAMP
) TIMESTAMP(ts)
PARTITION BY DAY;
There are two test data entries:
The requirement is to be able to query:
2024-12-03T09:12:00.000000Z
2024-12-03T09:11:00.000000Z
2024-12-03T09:10:00.000000Z
…
2024-12-03T09:03:00.000000Z
The requirement is to obtain the 10 K-line data, where there were no transactions from 2024-12-03T00:12:46.000000Z to 2024-12-03T09:12:00.000000Z. Therefore, the expected result is that the open, high, low, and close values for the period from 2024-12-03T09:03 to 2024-12-03T09:12 are filled with price=2068.94
, and the other amount
values are filled with 0. How can this be achieved?
This is the SQL I tried to write, but I did not get the expected result. Please guide me, and I would greatly appreciate it.
SELECT
first(price) AS open,
max(price) AS high,
min(price) AS low,
last(price) AS close,
sum(amount) AS total_volume,
count(amount) AS total_count,
ts AS kline_time
FROM
new_trades
WHERE
symbol = 'ETH-USDT'
AND ts <= '2024-12-03T09:12:31.612000Z'
SAMPLE BY 1m
FILL(PREV,PREV,PREV,PREV,0,0)
ORDER BY kline_time DESC
LIMIT 10;