# Query data with conditions on multiple fields

**URL:** <https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305>\
**Category:** Fluxlang\
**Tags:** flux\
**Created:** [January 11, 2022, 4:21am UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305 "2022-01-11T04:21:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ycsin/32/9191_2.png) [@ycsin](https://community.influxdata.com/u/ycsin)\
**Post date:** [January 11, 2022, 4:21am UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/1 "2022-01-11T04:21:53Z")

</div>

Hi all,

I have a data like this:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/6/6153be0b52386447414c9161db0fc9fa64cc308b.png)

`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!

---

<div class="post-metadata">

**Author:** ![nicgrobler](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nicgrobler/32/9193_2.png) [@nicgrobler](https://community.influxdata.com/u/nicgrobler)\
**Post date:** [January 11, 2022, 9:42am UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/2 "2022-01-11T09:42:04Z")

</div>

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

> **[Query using conditional logic in Flux | InfluxDB Cloud Documentation](https://docs.influxdata.com/influxdb/cloud/query-data/flux/conditional-logic/#evaluating-conditional-expressions)**
>
> This guide describes how to use Flux conditional expressions, such as if, else, and then, to query and transform data. Flux evaluates statements from left to right and stops evaluating once a condition matches.

---

<div class="post-metadata">

**Author:** ![ycsin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ycsin/32/9191_2.png) [@ycsin](https://community.influxdata.com/u/ycsin)\
**Post date:** [January 11, 2022, 2:47pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/3 "2022-01-11T14:47:14Z")

</div>

Yeah, I know how to filter base on a single `_field`, and I can set multiple conditions on that `_field` using `and` / `or`  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/3/3d27d9661418c838124fa48b04f6add74cad92e0.png)

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?

---

<div class="post-metadata">

**Author:** ![nicgrobler](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nicgrobler/32/9193_2.png) [@nicgrobler](https://community.influxdata.com/u/nicgrobler)\
**Post date:** [January 11, 2022, 3:41pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/4 "2022-01-11T15:41:01Z")

</div>

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())?

---

<div class="post-metadata">

**Author:** ![mhall119](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mhall119/32/3898_2.png) [@mhall119](https://community.influxdata.com/u/mhall119)\
**Post date:** [January 11, 2022, 6:10pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/5 "2022-01-11T18:10:48Z")

</div>

> [@ycsin](#):
>
> 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

You can combine `and` with `or` and parenthesis like this:

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

```

---

<div class="post-metadata">

**Author:** ![ycsin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ycsin/32/9191_2.png) [@ycsin](https://community.influxdata.com/u/ycsin)\
**Post date:** [January 12, 2022, 2:04pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/6 "2022-01-12T14:04:49Z")

</div>

@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](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/ed4b5bfce100e1951da93278217dbd6c43b5f240.png)

---

<div class="post-metadata">

**Author:** ![nicgrobler](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nicgrobler/32/9193_2.png) [@nicgrobler](https://community.influxdata.com/u/nicgrobler)\
**Post date:** [January 12, 2022, 2:21pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/7 "2022-01-12T14:21:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicgrobler](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nicgrobler/32/9193_2.png) [@nicgrobler](https://community.influxdata.com/u/nicgrobler)\
**Post date:** [January 12, 2022, 2:27pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/8 "2022-01-12T14:27:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ycsin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ycsin/32/9191_2.png) [@ycsin](https://community.influxdata.com/u/ycsin)\
**Post date:** [January 12, 2022, 2:38pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/9 "2022-01-12T14:38:31Z")

</div>

![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/ecdaf3d1eb646dcc45b5477cc3d557101a0d402f.png)

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.

---

<div class="post-metadata">

**Author:** ![mhall119](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mhall119/32/3898_2.png) [@mhall119](https://community.influxdata.com/u/mhall119)\
**Post date:** [January 12, 2022, 4:32pm UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/10 "2022-01-12T16:32:36Z")

</div>

> [@ycsin](#):
>
> 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?

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.

> [@ycsin](#):
>
> I’m trying to filter the two highlighted row out from the table.

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

---

<div class="post-metadata">

**Author:** ![ycsin](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ycsin/32/9191_2.png) [@ycsin](https://community.influxdata.com/u/ycsin)\
**Post date:** [January 13, 2022, 1:42am UTC](https://community.influxdata.com/t/query-data-with-conditions-on-multiple-fields/23305/11 "2022-01-13T01:42:26Z")

</div>

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.
