admin管理员组文章数量:1394165
I have a question regarding the SAMPLE BY query method, which live demo has in the OHLC downsample example. I copied the query down below
/* Aggregations for the BTC-USDT for today downsampled in 15-minute intervals.
We use the SQL extension SAMPLE BY to aggregate data at regular intervals. QuestDB
ingests live market data from the OKX API. */
SELECT
timestamp, symbol,
first(price) AS open,
last(price) AS close,
min(price),
max(price),
sum(amount) AS volume
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
SAMPLE BY 15m;
As in the demo, the records are grouped in a 15 minute interval. For example, records between 2025-03-22T00:00:00.000000Z
and 2025-03-22T00:15:00.000000Z
will be aggregated with timestamp of 2025-03-22T00:00:00.000000Z
.
However, I want to ask if it’s possible to give this aggregation 2025-03-22T00:15:00.000000Z
(which is the right bound of the interval rather than left)?
I have a question regarding the SAMPLE BY query method, which live demo has in the OHLC downsample example. I copied the query down below
/* Aggregations for the BTC-USDT for today downsampled in 15-minute intervals.
We use the SQL extension SAMPLE BY to aggregate data at regular intervals. QuestDB
ingests live market data from the OKX API. */
SELECT
timestamp, symbol,
first(price) AS open,
last(price) AS close,
min(price),
max(price),
sum(amount) AS volume
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
SAMPLE BY 15m;
As in the demo, the records are grouped in a 15 minute interval. For example, records between 2025-03-22T00:00:00.000000Z
and 2025-03-22T00:15:00.000000Z
will be aggregated with timestamp of 2025-03-22T00:00:00.000000Z
.
However, I want to ask if it’s possible to give this aggregation 2025-03-22T00:15:00.000000Z
(which is the right bound of the interval rather than left)?
1 Answer
Reset to default 0Of course, no problem.
We can simply shift the timestamp in the SELECT
:
SELECT
dateadd('m', 15, timestamp) timestamp, symbol,
first(price) AS open,
last(price) AS close,
min(price),
max(price),
sum(amount) AS volume
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
SAMPLE BY 15m;
Note that on executing this query, QuestDB is not displaying the timestamp in green on the web console. This is because we are not outputting the original designated timestamp, but a derived column. If you are not going to use this query in a subquery, then you are good to go. But supposing you want to use the output of this query in a subquery that requires a designated timestamp, you could do something like this to force sort order by the derived timestamp column, so everything would work.
(
SELECT
dateadd('m', 15, timestamp) timestamp, symbol,
first(price) AS open,
last(price) AS close,
min(price),
max(price),
sum(amount) AS volume
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
SAMPLE BY 15m
) ORDER BY timestamp;
本文标签: databaseHow to put right interval bound of timestamp in SAMPLE BY queryStack Overflow
版权声明:本文标题:database - How to put right interval bound of timestamp in SAMPLE BY query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744086812a2588674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论