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

如何将QuestDB中查询到的Timestamp转换为Int类型?

解决QuestDB时间戳转整数用于Prometheus sql-exporter的问题

QuestDB中的timestamp类型本质是微秒级的64位整数,你之前尝试转int无效是因为32位int的范围不足以容纳微秒级时间戳(会溢出),必须转换为64位的long类型。以下是两种可行的查询写法:

方法1:直接类型转换

使用cast函数将timestamp转为long类型,这是最直接的方式:

SELECT cast(timestamp AS long) AS latest_timestamp FROM <table> ORDER BY timestamp DESC LIMIT 1

方法2:使用内置时间戳函数

QuestDB提供了专门的函数返回Unix时间戳整数,根据你需要的精度选择:

  • 微秒级(和QuestDB原生timestamp精度一致):
SELECT unix_timestamp_micros(timestamp) AS latest_timestamp FROM <table> ORDER BY timestamp DESC LIMIT 1
  • 秒级(如果Prometheus指标不需要微秒精度):
SELECT unix_timestamp_seconds(timestamp) AS latest_timestamp FROM <table> ORDER BY timestamp DESC LIMIT 1

这两种方式返回的都是整数类型,能直接被sql-exporter读取并上报给Prometheus。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:24:52