使用 Flux 查询基于组查找总和 - InfluxDB
Finding the Sum based on Group using Flux Query - InfluxDB
我正在尝试使用通量查询语言来查找基于人的列的总和。
如果我有以下输入 table:
如何使用 Flux 查询获得以下输出 table:
到目前为止,我已经尝试过类似的操作,但出现错误:
from: (bucket: "example")
|> range(start:v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r)=> r["_measurement"] == "test")
|> group(columns: r["person"])
|> reduce( fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0})
你几乎走对了路。您已经按名称分组,现在您需要使用函数 sum
。注意最后一个函数 - |> group()
它只是为了联合 table 到一个视图。
|> group(columns: ["person"])
|> sum(column: "hoursSpent")
|> group()
我提供了完整的调试查询:
import "array"
data = array.from(rows: [
{person: "John Smith", sport: "Cycling", hoursSpent: 5},
{person: "John Smith", sport: "Hiking", hoursSpent: 6},
{person: "John Smith", sport: "Swimming", hoursSpent: 1},
{person: "John Smith", sport: "Dancing", hoursSpent: 2},
{person: "Nancy Jones", sport: "Badminton", hoursSpent: 10},
{person: "Nancy Jones", sport: "Soccer", hoursSpent: 31},
{person: "Nancy Jones", sport: "Basketball", hoursSpent: 8},
{person: "Trevor John", sport: "Baseball", hoursSpent: 24},
{person: "Trevor John", sport: "Water Polo", hoursSpent: 2},
])
data
|> group(columns: ["person"])
|> sum(column: "hoursSpent")
|> group()
我正在尝试使用通量查询语言来查找基于人的列的总和。 如果我有以下输入 table:
如何使用 Flux 查询获得以下输出 table:
到目前为止,我已经尝试过类似的操作,但出现错误:
from: (bucket: "example")
|> range(start:v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r)=> r["_measurement"] == "test")
|> group(columns: r["person"])
|> reduce( fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0})
你几乎走对了路。您已经按名称分组,现在您需要使用函数 sum
。注意最后一个函数 - |> group()
它只是为了联合 table 到一个视图。
|> group(columns: ["person"])
|> sum(column: "hoursSpent")
|> group()
我提供了完整的调试查询:
import "array"
data = array.from(rows: [
{person: "John Smith", sport: "Cycling", hoursSpent: 5},
{person: "John Smith", sport: "Hiking", hoursSpent: 6},
{person: "John Smith", sport: "Swimming", hoursSpent: 1},
{person: "John Smith", sport: "Dancing", hoursSpent: 2},
{person: "Nancy Jones", sport: "Badminton", hoursSpent: 10},
{person: "Nancy Jones", sport: "Soccer", hoursSpent: 31},
{person: "Nancy Jones", sport: "Basketball", hoursSpent: 8},
{person: "Trevor John", sport: "Baseball", hoursSpent: 24},
{person: "Trevor John", sport: "Water Polo", hoursSpent: 2},
])
data
|> group(columns: ["person"])
|> sum(column: "hoursSpent")
|> group()