# Where clauses in Flux query

**URL:** <https://community.influxdata.com/t/where-clauses-in-flux-query/23909>\
**Category:** Fluxlang\
**Tags:** flux\
**Created:** [February 18, 2022, 10:08am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909 "2022-02-18T10:08:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aritra666B](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aritra666B](https://community.influxdata.com/u/Aritra666B)\
**Post date:** [February 18, 2022, 10:08am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/1 "2022-02-18T10:08:22Z")

</div>

Hi guys,  
Am trying to add where clause in my flux query but it’s not working. I have tried the following methods but no result

1. from(bucket: “L\_OEEA\_COMPACT”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r["\_measurement"] == “RT\_Dashboard”)  
|\> filter(fn: (r) =\> r["\_field"] == “Availability”)  
|\> filter(fn: (r) =\> r[“StationID”] == 2)  
|\> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)  
|\> yield(name: “last”)

2. from(bucket: “L\_OEEA\_COMPACT”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r["\_measurement"] == “RT\_Dashboard”)  
|\> filter(fn: (r) =\> r["\_field"] == “Availability” and r[“StationID”] == 2)  
|\> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)  
|\> yield(name: “last”)

---

<div class="post-metadata">

**Author:** ![asthomas](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@asthomas](https://community.influxdata.com/u/asthomas)\
**Post date:** [February 18, 2022, 11:55am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/2 "2022-02-18T11:55:49Z")

</div>

I think you have to do something like this:

```auto
from(bucket: "L_OEEA_COMPACT")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r._measurement == "RT_Dashboard" and r._field =~ /Availability|StationID/)
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
    |> filter(fn: (r) => r.StationID == 2)
    |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
    |> yield(name: "last")

```

I’m not sure about the second filter, so try running without it just to show that you get some data.

---

<div class="post-metadata">

**Author:** ![Aritra666B](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aritra666B](https://community.influxdata.com/u/Aritra666B)\
**Post date:** [February 18, 2022, 12:10pm UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/3 "2022-02-18T12:10:19Z")

</div>

> [@Aritra666B](#):
>
> Availability

Nope, didn’t work… Gives error \_value column doesn’t exists. If I try to replace it with the required field name, then also same error

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [February 18, 2022, 10:15pm UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/4 "2022-02-18T22:15:50Z")

</div>

Hello @asthomas,  
What would your where clause look like in SQL or InfluxQL? I’m having trouble understanding what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![asthomas](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@asthomas](https://community.influxdata.com/u/asthomas)\
**Post date:** [February 19, 2022, 12:07am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/5 "2022-02-19T00:07:09Z")

</div>

Hello @Anaisdg. I don’t know either. I was taking a reasonable guess from @Aritra666B’s question. It looks like the goal is to create records that contain both StationID and Availability as columns, for any record where StationID == 2. I am guessing that StationID and Availability are fields, but I don’t know the data schema.

---

<div class="post-metadata">

**Author:** ![Aritra666B](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aritra666B](https://community.influxdata.com/u/Aritra666B)\
**Post date:** [February 21, 2022, 4:10am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/6 "2022-02-21T04:10:05Z")

</div>

Hello @Anaisdg,

My target is to get values of columns like Availability based on the StationID value 1 or 2.  
In older version(1.8) I had no trouble doing this, but since we are now unable to do it using Flux query.

My SQL query looked like this:  
SELECT $\_\_time(EntryTimeStamp),  
Availability as ‘Availability’  
FROM dbo.RT\_DashBoard WHERE StationID=$StationID

---

<div class="post-metadata">

**Author:** ![Aritra666B](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aritra666B](https://community.influxdata.com/u/Aritra666B)\
**Post date:** [February 21, 2022, 10:25am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/7 "2022-02-21T10:25:40Z")

</div>

So I was trying something for the where clause:  
from(bucket: “L\_OEEA\_COMPACT”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r["\_measurement"] == “RT\_ProductionLog”)  
|\> filter(fn: (r) =\> r["\_field"] == “StationID” or r["\_field"] == “Mode”)  
|\> pivot(rowKey: ["\_time"], columnKey: ["\_field"], valueColumn: “\_value”)  
|\> map(fn: (r) =\> ({ r with \_value: r.Mode }))  
|\> filter(fn: (r) =\> r.StationID == ${StationID})  
|\> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)  
|\> yield(name: “last”)

Now this works fine for integer fields, but for string type it returns 2 twice the values.

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [February 22, 2022, 5:12pm UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/8 "2022-02-22T17:12:01Z")

</div>

Hello @Aritra666B.  
What do you mean by

> 2 twice the values

You’re saying the string values are getting multiplied by 2?  
That seems implausible since you’re not multiplying by 2 anywhere.  
You can convert all the values to int with the toInt() function before the pivot function.

> **[toInt() function | Flux Documentation](https://docs.influxdata.com/flux/v0/stdlib/universe/toint/)**
>
> toInt() converts all values in the \_value column to integer types.

You might also want to be aware of:

> **[schema.fieldsAsCols() function | Flux Documentation](https://docs.influxdata.com/flux/v0/stdlib/influxdata/influxdb/schema/fieldsascols/)**
>
> schema.fieldsAsCols() is a special application of pivot() that pivots input data on \_field and \_time columns to align fields within each input table that have the same timestamp.

---

<div class="post-metadata">

**Author:** ![Aritra666B](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Aritra666B](https://community.influxdata.com/u/Aritra666B)\
**Post date:** [February 23, 2022, 4:07am UTC](https://community.influxdata.com/t/where-clauses-in-flux-query/23909/9 "2022-02-23T04:07:20Z")

</div>

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

from(bucket: “L\_OEEA\_COMPACT”)

|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)

|\> filter(fn: (r) =\> r["\_measurement"] == “RT\_ProductionLog”)

|\> filter(fn: (r) =\> r["\_field"] == “StationID” or r["\_field"] == “Mode”)

|\> pivot(rowKey: ["\_time"], columnKey: ["\_field"], valueColumn: “\_value”)

|\> map(fn: (r) =\> ({ r with \_value: r.Mode }))

|\> filter(fn: (r) =\> r.StationID == 2)

|\> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)

|\> yield(name: “last”)
