Count() sum() on the same measurement

Hello All,

simple question but still I have problem to realize it.
there is table with “anomaly” field which has value 1 or 0.
Now I need to perform percentage calculation how many anomaly with value=1 appears in relation to all anomalies (values 0 +1).
So the idea is simple I need sum() / count() on the same measurement and the same field (anomaly).

how to do in Flux 2.0 ?

Please help.


hello @Tom999,
Yes! Sorry for the delay. Please feel free to tag me in the future.
You could do something like:

import "array"

rows = [{_time: 1, _field: "foo",  _value: 1}, {_time: 2, _field: "foo", _value: 0}, {_time: 3,_field: "foo", _value: 1}]
data = array.from(rows: rows)
total = (data
    |> count() 
    |> findRecord( fn: (key) => true, idx: 0))._value

one = (data
 |> filter(fn: (r) => r._value == 1)
 |> count()
 |> findRecord( fn: (key) => true, idx: 0))._value

array.from(rows: [{percent: float(v: one)/ float(v:total)*100.0 }])

does that work for you?

1 Like

Also related to: