Query a counter of distinct values

Hi, I’m looking for an influx version of Python’s Counter.
Basically, a way of counting the number of values for each distinct values (also called occurrences).

I already found two topics related to my question:

With no conclusive answers.

Hello @Thomas_Jalabert,

I was able to do this in flux with the following script:

   from(bucket: "test")
      |> range(start: dashboardTime)
      |> filter(fn: (r) => r._field == "value")
      |> group(columns: ["_value"])
      |> map(fn: (r) => ({_time: r._time, index: 1}))
      |> cumulativeSum(columns: ["index"]) 
      |> last()

Where my data in line protocol looks like:

test,tagkey=occ, value=1
test,tagkey=occ, value=5
test,tagkey=occ, value=5
test,tagkey=occ, value=1
test,tagkey=occ, value=1

This feels a little hacky, but hopefully, it will carry you through while I look for a more elegant solution.

The distinct() function could be useful too.

1 Like

@Thomas_Jalabert at the very least, at least now you’ve been introduced to some pretty useful flux functions. If you want to write more flux scripts, this will be your best resource.

Also, please take a look at this testdata repo . It has examples of how each flux function transforms small example datasets.

Thank you @Anaisdg!

I was hoping for a solution using InfluxQL.
I never used flux before, surely I will take a look!

Thanks again for the various ressources.

It worked for me! Many Thanks Anaisdg!!!

One question… is it possible to filter only a value of a field? something like this is not working for me:

  |> filter(fn: (r) =>
          r._measurement == "syslog" and (
          r._field=="search_text" and r.field_name=="something"