Filter data in table based on value of one column, keeping other data

Hello,

What I’m trying to do is filter a table in InfluxDB based on the value of a single column, whilst keeping the data from all of the other values of the other columns.

For example, consider this table:

A = 1, B = %, C = hello
A = 2, B = $, C = world
A = 3, B = #, C = hello
A = 4, B = *, C = world
A = 5, B = @, C = hello

I’d like to be able to query this table and pull out all of the data where C == world, leaving
A = 2, B = $, C = world
A = 4, B = *, C = world

behind. However, the world would actually be the value of a variable generated by my program.

Is this possible? I’d like to use Flux with this - this is part of a Query to the database from golang.

Have you tried the obvious:

select A,B,C from table where C=‘world

?

or even, if those are all the columns you have:

select * from table where C=‘world

Antony.

Apologies, I should have stated that I’d like to use Flux with this - this is part of a Query to the database from golang.

@jos Just use the equality operator (==) when filtering:

data
  |> filter(fn: (r) => r.C == "world")

filter() doesn’t modify any columns/values, it simply filters out rows that resolve to false when evaluated against the predicate function (fn). r represents the row. r.C represents the value of the C column.

@scott If I’m using golang to make the query, is it possible to substitute the “world” there with the value of a variable?

Additionally, when trying to do this in the script editor, I am receiving an error:

  from(bucket: "myBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "DataName")
  |> filter(fn: (r) => r["_field"] == "A" or r["_field"] == "B" or r["_field"] == "C" or r["_field"] == "D")
  |> filter(fn: (r) => r.A== "world"

Results in

compilation failed: loc 5:6-5:71: expected RPAREN, got EOF

After speaking through the slack channel, the solution here was to use the pivot() function. Using a variable will most likely occur by constructing the query string at runtime. Alternatively, map() can be used.

My code would look something like this:

    from(bucket: "MyBucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "DataArea")
  |> filter(fn: (r) => r["_field"] == "A" or r["_field"] == "B" or r["_field"] == "C" or r["_field"] == "D")
  |> pivot(
    rowKey:["_time"],
    columnKey: ["_field"],
    valueColumn: "_value"
  )
  > filter(fn: (r) => r.A== "world")

The expected RPAREN, got EOF error was because I was missing a ) at the end of my query in Golang.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.