# How do I select multiple field columns with flux?

**URL:** <https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314>\
**Category:** Fluxlang\
**Created:** [September 20, 2019, 5:02am UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314 "2019-09-20T05:02:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj\_m](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/manoj_m/32/3652_2.png) [@manoj\_m](https://community.influxdata.com/u/manoj_m)\
**Post date:** [September 20, 2019, 5:02am UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/1 "2019-09-20T05:02:37Z")

</div>

equivalent influxQL:

`SELECT a, b where a = 'hello' and b > 10;`

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [October 3, 2019, 10:06pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/2 "2019-10-03T22:06:04Z")

</div>

@manoj_m, you’d do it with `filter()`:

```javascript
// ...
  |> filter(fn: (r) => r.a == "hello" and r.b > 10)

```

---

<div class="post-metadata">

**Author:** ![Aga-Schwarz](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Aga-Schwarz](https://community.influxdata.com/u/Aga-Schwarz)\
**Post date:** [December 5, 2019, 3:19pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/3 "2019-12-05T15:19:00Z")

</div>

@scott Is this correct for value-fields (no-tag fields)? I am trying to execute a simple select via Chronograf but it does not seem to be working the way you explained.

Assumed I would have inserted data with  
**`insert test a=10,b=40`**  
**`insert test a=20,b=30`**  
**`insert test a=20,b=40`**

Now I want to retrieve BOTH a and b variables of the second datapoint.  
This is very simple in influx:  
**`select a,b from test where a>10 AND b=30`**

But I have absolutely no idea how to acomplish this in flux?  
At least via Chronograf it appears that every value is returned as stand-alone and can be addressed via \_field for field name and \_value for the actual value.

This gets me to:

**`from(bucket: "test/autogen")`**  
**`|> range(start: MyStartTime, stop: MyEndTime)`**  
**`|> filter(fn: (r) => r._measurement == "test" )`**  
**`|> filter(fn: (r) => r._field == "a" )`**  
**`|> filter(fn: (r) => r._value > 10)`**

But I have no idea how to combine this with the value of field b. Your solution suggests to use  
r.a, r.b but this does not work in case a, b are regular fileds.

Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [December 5, 2019, 4:04pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/4 "2019-12-05T16:04:52Z")

</div>

Sorry, I’m not sure why, but in my initial answer I assumed `a` and `b` were tags, not fields. You can use `pivot()` to pivot `a` and `b` fields into the same rows:

```javascript
from(bucket: "test/autogen")
  |> range(start: MyStartTime, stop: MyEndTime)
  |> filter(fn: (r) => r._measurement == "test" and r._field =~ /a|b/ )
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> filter(fn: (r) => r.a > 10 and r.b == 30)

```

---

<div class="post-metadata">

**Author:** ![Aga-Schwarz](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Aga-Schwarz](https://community.influxdata.com/u/Aga-Schwarz)\
**Post date:** [December 5, 2019, 5:44pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/5 "2019-12-05T17:44:20Z")

</div>

@scott Thanks a lot for your speedy reply! It works perfectly now!

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [December 5, 2019, 6:03pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/6 "2019-12-05T18:03:02Z")

</div>

@Aga-Schwarz Happy to help!

---

<div class="post-metadata">

**Author:** ![Scav](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scav/32/6881_2.png) [@Scav](https://community.influxdata.com/u/Scav)\
**Post date:** [March 8, 2021, 9:46am UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/7 "2021-03-08T09:46:11Z")

</div>

Hi Scott, I don’t want to do the same question again but basically i have the same issue.  
I have been reading carefully the question and the reply but I’m struggling on this from days.  
I have a bucket with 10 columns and i would like to get two/three of them.

This is my query:

query = ’ from(bucket:“b\_box”)’   
'|\> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z) ’  
‘|\> filter(fn: ® =\> r.\_field =~ /“st\_millis”|“st\_level”/)’   
‘|\> pivot(rowKey: ["\_time"], columnKey: ["\_field"], valueColumn: “\_value”)’

I have the data on the range indicated but the result is always nothing. I can query one column with no issues, apply a filter and others but getting two columns… no way. Could you please give me some tips? Thanks!

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [March 8, 2021, 3:33pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/8 "2021-03-08T15:33:06Z")

</div>

@Scav It likely means that your timestamps don’t align perfectly so there are no common timestamps to pivot on. One thing you can try is using [`truncateTimeColumn()`](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/truncatetimecolumn/) to reduce the precision of your timestamps. I don’t know how precise your data is, but the following example rounds timestamps off to seconds:

```javascript
query = from(bucket:“b_box”)
  |> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z)
  |> filter(fn: (r) => r._field =~ /st_millis|st_level/)
  |> truncateTimeColumn(unit: 1s)
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: “_value”)

```

---

<div class="post-metadata">

**Author:** ![Scav](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scav/32/6881_2.png) [@Scav](https://community.influxdata.com/u/Scav)\
**Post date:** [March 8, 2021, 4:44pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/9 "2021-03-08T16:44:09Z")

</div>

Hi Scott,

This si the python code that I’m using to insert the data in Influx. I’m receving a JSON message, opening it up and feeding the db so the time stamp is exactly the same.

```
  st_price = str(js_msg["st_price"])
                st_millis = str(js_msg["st_millis"])
                st_level = str(js_msg["st_level"])

   data = "mem,_box=a " \
                       "st_millis=" + st_millis + ","\
                       "st_level=" + st_level + "," \
                       st_millis + "000000"

                write_api.write(b_box, org, data)

```

st\_millis contain the millis, the bad thing that I’m doing is to add the “000000” but I’m sure 100% that the time stamp is the same because is the same message and they are added all together.

I tried your suggestion but still nothing, unfortunatelly.

```
query = ' from(bucket:"b_box")' \
    '|> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z) '\
    '|> filter(fn: (r) => r._field =~ /"st_millis"|"st_level"/ )' \
    '|> truncateTimeColumn(unit: 1s)'\
    '|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")' 

```

What else could I check?  
Many thanks!

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [March 8, 2021, 4:59pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/10 "2021-03-08T16:59:23Z")

</div>

@Scav Remove of the quotes from your Flux regex. It’s trying to match those as literal characters in your field names.

```python
# ...
  '|> filter(fn: (r) => r._field =~ /st_millis|st_level/ )' \

```

---

<div class="post-metadata">

**Author:** ![Scav](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scav/32/6881_2.png) [@Scav](https://community.influxdata.com/u/Scav)\
**Post date:** [March 8, 2021, 5:20pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/11 "2021-03-08T17:20:47Z")

</div>

nope, it doesn’t like it.

```
return self.values. __getitem__ (key)
KeyError: '_field'
Process finished with exit code 1

```

What can i check on the UI, that maybe can help?

Thanks

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [March 8, 2021, 5:30pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/12 "2021-03-08T17:30:14Z")

</div>

That looks like a Python error, not a Flux error, so it’s something in the Python code. That regex in Flux won’t match against anything in InfluxDB unless your field keys actually have quotes in them.

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [March 8, 2021, 5:33pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/13 "2021-03-08T17:33:06Z")

</div>

Does changing your string formatting change anything?

```python
query = ''' from(bucket: "b_box")
    |> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z)
    |> filter(fn: (r) => r._field =~ /st_millis|st_level/ )
    |> truncateTimeColumn(unit: 1s)
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
'''

```

---

<div class="post-metadata">

**Author:** ![Scav](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scav/32/6881_2.png) [@Scav](https://community.influxdata.com/u/Scav)\
**Post date:** [March 8, 2021, 5:37pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/14 "2021-03-08T17:37:40Z")

</div>

This work fine:

```
query = ' from(bucket:"b_box")' \
    '|> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z) '\
    '|> filter(fn: (r) => r._field == "st_level")'

```

And I’m using **"st\_level"**

Apologies, i got a bit lost what did you mean by “changing my string formatting”?

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [March 8, 2021, 6:21pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/15 "2021-03-08T18:21:58Z")

</div>

The Python string formatting. Right now you’re using single-line string formatting (`''`). Using triple quotes allows you to do multi-line strings in Python.

---

<div class="post-metadata">

**Author:** ![Scav](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scav/32/6881_2.png) [@Scav](https://community.influxdata.com/u/Scav)\
**Post date:** [March 8, 2021, 6:32pm UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/16 "2021-03-08T18:32:18Z")

</div>

> [@scott](#):
>
> ```auto
> query = ''' from(bucket: "b_box")
> |> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z)
> |> filter(fn: (r) => r._field =~ /st_millis|st_level/ )
> |> truncateTimeColumn(unit: 1s)
> |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
> '''
> 
> ```

if i write it like that:

```
query = ''' from(bucket: "b_box")
    |> range(start: 2021-03-05T18:55:00Z, stop: 2021-03-05T18:56:00Z)
    |> filter(fn: (r) => r._field =~ /"st_millis"|"st_level"/ )
    |> truncateTimeColumn(unit: 1s)
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
'''

```

same as before nothing back. If i write it as you suggested, i have an error:

```
/usr/bin/python3.6 /InfluxQuery_1.py
Traceback (most recent call last):
    results.append((record.get_field(), record.get_value()))
    return self["_field"]
    return self.values. __getitem__ (key)
KeyError: '_field'

```

Process finished with exit code 1

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Voodoom](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/voodoom/32/8491_2.png) [@Voodoom](https://community.influxdata.com/u/Voodoom)\
**Post date:** [December 2, 2021, 7:17am UTC](https://community.influxdata.com/t/how-do-i-select-multiple-field-columns-with-flux/11314/17 "2021-12-02T07:17:04Z")

</div>

Thanks @scott,

Your reply helped me with a problem, I was facing for a while. I appreciate your help.
