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

CnosDB中不同精度Timestamp类型值对比返回错误结果

问题解决方法

问题核心是时间戳类型不匹配:你的time字段是纳秒级时间戳(TimestampNanosecond),但查询中用to_timestamp_millis()生成的是毫秒级时间戳(TimestampMillisecond),两者直接比较会导致精度匹配错误,无法返回正确结果。

两种可行修正方案:

方案1:将毫秒数转换为纳秒级时间戳(推荐,保留原字段精度)

把毫秒数乘以1000000转成纳秒数,再用to_timestamp_nanos()生成对应类型的时间戳,确保和time字段类型一致:

select * 
from user_queries 
where time <= to_timestamp_nanos(1680856751413 * 1000000) 
limit 1;

方案2:将纳秒字段转换为毫秒级时间戳(会丢失纳秒精度)

如果业务场景允许忽略纳秒精度,可以把time字段转成毫秒级后再比较:

select * 
from user_queries 
where to_timestamp_millis(time) <= to_timestamp_millis(1680856751413) 
limit 1;

内容的提问来源于stack exchange,提问作者cosmic

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:13:17