Query data with conditions on multiple fields

Hi all,

I have a data like this:

fix has the value of either 0, 1, 2 or 255
ttfx is a positive integer number

I’d like to get the total number of occurrence where:
fix == 1 && ttfx > 0

How can I do it in Flux 2.0.x query?

Thanks!

Did you try using ‘and’ instead of ‘&&’ - it has a few examples here:

Yeah, I know how to filter base on a single _field, and I can set multiple conditions on that _field using and / or
image

But what I need is different conditions for multiple _field: I need to filter out and count the number of cases where:

  • When _field == “fix”, _value == 1, and
  • When _field == “ttff”, _value != 0

Is this possible in InfluxDB?

pretty sure that you cannot do this in one filter - but, you could create two functions (not efficient, but it could work), one for each of your two filter combinations in your comment, then join the two streams back together (or even easier, just add the results together - from your count())?

1 Like

You can combine and with or and parenthesis like this:

 |> filter(fn: (r) => (r._field == "fix" and r._value == 1) or (r._field="ttff" and r._value != 0))
3 Likes

@nicgrobler Could you show how it can be done? I don’t mind lower efficiency. How do I get their intersection if I join two streams back together.

@mhall119 thanks for your reply, but it doesn’t seem to work

I’m trying to filter the two highlighted row out from the table.
image

this is nice - couldn’t see this in the docs (using brackets like this to combine) - and no instance on which to test. :+1:

change the query to use “) and (” instead of “) or (” assuming you want it to return only the two highlighted rows.

I think after the first condition the data left only has “fix” column, then the second condition asked for “ttff” and then there’s no data left?

I found that it can be worked around using two queries, then do the actual conditional filtering part using Grafana’s transform, but I’d like to have filtered data straight from the db to prevent processing data in the frontend.

That’s correct, you’ll only have one value for r._field so using what is essentially r._field == "fix" and r._field == "ttff" would always be false and therefore would filter out every record.

You want to exclude those roles, or return only those roles?

I want to return only the highlighted rows.
And eventually I want to count the number of returned rows, for the example that I’ve provided, it should return 2.