You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QuestDB是否支持LAG窗口函数?有无等效替代方案?

在QuestDB中实现相邻行值差值计算

你遇到的LAG()函数报错问题,是因为QuestDB对标准SQL窗口函数的支持有特定要求,但它提供了更轻量的替代方案——内置的prev()函数,专门用来获取上一行的列值。

直接用下面的写法就能实现需求:

SELECT timestamp, price - prev(price) AS price_diff
FROM trades
ORDER BY timestamp;

prev()函数会自动遵循查询的ORDER BY顺序,帮你拿到当前行的上一行price值,省去了标准SQL里OVER子句的繁琐。如果需要按特定字段分组计算差值(比如按交易对分组),可以加上分区语法:

SELECT 
  trade_pair,
  timestamp, 
  price - prev(price) OVER (PARTITION BY trade_pair ORDER BY timestamp) AS price_diff
FROM trades
ORDER BY trade_pair, timestamp;

内容的提问来源于stack exchange,提问作者Nick The Greek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 00:02:04