I am new to influx DB , i tried framing flux query which gives output columns as mean, count and 95 percentile on a single column but i didn’t get the desired output, could anyone please help me with flux query
Sql query looks like
select avg(colA),count(colA),95percentile(colA) from _measurement
scott
July 29, 2024, 5:10pm
2
@Sravs_Sekhar This is how I’d do it:
data = () => from(bucket: "exmaple-bucket")
|> range(start: -1d)
|> filter(fn: (r) => r._measurement == "example-measurement")
|> filter(fn: (r) => r._field == "colA")
avg = data() |> mean() |> set(key: "_field", value: "avg")
count = data() |> count() |> set(key: "_field", value: "count")
pct = data() |> quantile(q: 0.95) |> set(key: "_field", value: "95percentile")
union(tables: [avg, count, pct])
|> pivot(rowKey: ["_start"], columnKey: ["_field"], valueColumn: "_value")
2 Likes
Thankyou for helping me , i tried the above query but an getting below error could you please do the needful
data = () => from(bucket: “example bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == “measurment” and r.ServiceName == " tag name ")
|> filter(fn: (r) => r._field == “field”)
|> filter(fn: (r) => r[“OrganizationCode”] =~ /^${OrganizationCode:regex}$/ )
|> group(columns: [“OrganizationCode”, “_measurement”], mode:“by”)
avg = data() |> mean() |> set(key: “_field”, value: “avg”)
count = data() |> count() set(key: “_field”, value: “count”)
pct = data() |> quantile(q: 0.95) set(key: “_field”, value: “95percentile”)
union(tables: [avg, count, pct])
|> pivot(rowKey: [“_start”], columnKey: [“_field”], valueColumn: “_value”)
Note: both servicename and OrganizationCode are tags
Below error :
error @8 :27-8:61: missing required argument tables error @9 :35-9:76: missing required argument tables
scott
August 6, 2024, 1:14am
4
Sorry, I had missed some pipe-forward’s (|>
) before the set()
calls. Try this:
data = () =>
from(bucket: "example bucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "measurment" and r.ServiceName == " tag name ")
|> filter(fn: (r) => r._field == "field")
|> filter(fn: (r) => r["OrganizationCode"] =~ /^${OrganizationCode:regex}$/ )
|> group(columns: ["OrganizationCode", "_measurement"], mode:"by")
avg = data() |> mean() |> set(key: "_field", value: "avg")
count = data() |> count() |> set(key: "_field", value: "count")
pct = data() |> quantile(q: 0.95) |> set(key: "_field", value: "95percentile")
union(tables: [avg, count, pct])
|> pivot(rowKey: ["_start"], columnKey: ["_field"], valueColumn: "_value")