如何在InfluxDB Flux查询中实现两值相除?报错求助
解决Flux查询中流对象无法做除法的错误
你的查询报错是因为odds和totalCount最终返回的是包含单条记录的流对象,而非直接的标量数值,Flux不支持流对象之间直接执行除法运算。
修正后的查询
odds = from(bucket: "me") |> range(start: -1h) |> filter(fn: (r) => r["_measurement"] == "games" and r["_field"] == "odds") |> filter(fn: (r) => r["_value"] > 2.0) |> count() |> toFloat() |> last() |> findRecord(fn: (key) => true, idx: 0) totalCount = from(bucket: "me") |> range(start: -1h) |> filter(fn: (r) => r["_measurement"] == "games" and r["_field"] == "odds") |> count() |> toFloat() |> last() |> findRecord(fn: (key) => true, idx: 0) percent = odds._value / totalCount._value * 100.0
关键修正点
- 添加
|> findRecord(fn: (key) => true, idx: 0):从流中提取唯一的一条记录,将流对象转换为可直接访问字段的记录对象 - 通过
._value访问记录中的数值:此时odds._value和totalCount._value是标量值,支持算术运算
另一种简化写法
也可以在提取数值时一步到位,直接获取标量:
odds = from(bucket: "me") |> range(start: -1h) |> filter(fn: (r) => r["_measurement"] == "games" and r["_field"] == "odds") |> filter(fn: (r) => r["_value"] > 2.0) |> count() |> toFloat() |> last() |> findRecord(fn: (key) => true, idx: 0) |> (r) => r._value totalCount = from(bucket: "me") |> range(start: -1h) |> filter(fn: (r) => r["_measurement"] == "games" and r["_field"] == "odds") |> count() |> toFloat() |> last() |> findRecord(fn: (key) => true, idx: 0) |> (r) => r._value percent = odds / totalCount * 100.0
内容的提问来源于stack exchange,提问作者somerandomusername
相关产品推荐
相关产品推荐

