@Sameer_Raj You’re correct in saying that each table is its own line in the graph. So the goal is to ensure that f1 and f2 are in separate tables, which they should be by default since _field
is part of the group key.
In your results, it’s important to understand that f1 and f2 are not column names. They are values in the _field
column. To get them to be their own columns, you need to use pivot()
right after your filter.
from(bucket: "SomeDB/autogen")
|> range($range)
|> filter(fn: (r) => r._measurement == "m1" and r._field =~ /^f1|f2/ )
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
I notice that you do two aggregates later in the query. The 2nd one won’t actually work since aggregate functions drop columns that 1) aren’t in the group key or 2) aren’t the specified aggregate column. So after you sum f1, f2 won’t exist anymore. You can use reduce()
to aggregate them and preserve them. Looking at your query as a whole, this should give you what you’re trying to do:
from(bucket: "SomeDB/autogen")
|> range($range)
|> filter(fn: (r) => r._measurement == "m1" and r._field =~ /^f1|f2/ )
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> window(every: 1m)
|> reduce(
identity: {f1_sum: 0.0, f2_sum: 0.0, f2_avg: 0.0, count: 0},
fn: (r, accumulator) => ({
f1_sum: r.f1 + accumulator.f1_sum,
f2_sum: r.f2 + accumulator.f2_sum,
f2_avg: (r.f2 + accumulator.f2_sum) / (accumulator.count + 1),
count: accumulator.count + 1
})
)
|> drop(columns: ["f2_sum", "count"])
|> rename(columns: {f1_sum: "f1", f2_avg: "f2"})
|> duplicate(column: "_stop", as: "_time")
|> window(every:inf)