# What is wrong with Flux query

**URL:** <https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614>\
**Category:** Fluxlang\
**Tags:** influxdb, flux\
**Created:** [April 21, 2023, 8:13am UTC](https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614 "2023-04-21T08:13:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Belly\_Musketier](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/belly_musketier/32/7093_2.png) [@Belly\_Musketier](https://community.influxdata.com/u/Belly_Musketier)\
**Post date:** [April 21, 2023, 8:13am UTC](https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614/1 "2023-04-21T08:13:15Z")

</div>

Hello all,  
I have started using the InfluxDB 2.x and currently facing problem in understanding the whole flux way of querying the data. In the earlier version in the WHERE clause I could use AND operator and enforce the condition for data what I am looking for for example `SELECT "field1",field2,field2" FROM "MEASUREMENT_x WHERE "field1">='condition1' AND "field1" <= 'condition2' AND "field2" >= 'condition3'` but unfortunately I am unable to translate the same query with flux. Can someone help me understand the flux query and how I can translate.  
Flux query  
`from(bucket: "some-bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "MEASUREMENT_x") |> filter(fn: (r) => r["_field"] == "field1" and r["_field"] == "field12" and r._value >= 12 and r._value <= 100 and r["_field"] == "field3" and r._value >= 0 and r._value <= 100 ) |> yield() `  
Thank you in advance!!

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [April 21, 2023, 10:33am UTC](https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614/2 "2023-04-21T10:33:45Z")

</div>

Hi @Belly_Musketier and kudos for trying to figure out Flux. I believe the key thing here is to use the `pivot()` function. See my examples below from my dataset that uses two fields (OutdoorHumidity and OutdoorTemp) that were both populated with the same timestamp (this is important because I use it in the pivot function):

Basic query with filters on the field names I have selected. Notice there are two table numbers on the far left: 0 and 1. If we added a third field, it would be labeled as 2.

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

Added a pivot function which moves the OutdoorHumidity and OutdoorTemp fields to their own columns:

```auto
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

```

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

Added two filters:

```auto
  |> filter(fn: (r) => r.OutdoorHumidity > 80)
  |> filter(fn: (r) => r.OutdoorTemp > 54)

```

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

Final query would look like this:

```auto
from(bucket: "Bucket1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "WeatherData")
  |> filter(fn: (r) => r["_field"] == "OutdoorHumidity" or r["_field"] == "OutdoorTemp")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> filter(fn: (r) => r.OutdoorHumidity > 80)
  |> filter(fn: (r) => r.OutdoorTemp > 54)
  |> yield(name: "your-query")

```

---

<div class="post-metadata">

**Author:** ![Belly\_Musketier](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/belly_musketier/32/7093_2.png) [@Belly\_Musketier](https://community.influxdata.com/u/Belly_Musketier)\
**Post date:** [April 21, 2023, 12:09pm UTC](https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614/3 "2023-04-21T12:09:06Z")

</div>

Hello @grant1 Thank you for your response I did think of using `Pivot()` and Also received the expected results. But later on read about that the `Pivot()` is performance and memory Intensive?? if yes is there a way to optimize the query with `Pivot()`

Thanks

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [April 21, 2023, 12:34pm UTC](https://community.influxdata.com/t/what-is-wrong-with-flux-query/29614/4 "2023-04-21T12:34:53Z")

</div>

Y’know, I have read the same things, yet have never experienced performance issues with using the pivot function. I think some queries are scanning such large datasets that it can come into play there, but for many of us, it never becomes an issue.
