# Group by a field with FLUX

**URL:** <https://community.influxdata.com/t/group-by-a-field-with-flux/22272>\
**Category:** InfluxDB 2\
**Tags:** influxdb, grafana, query, flux\
**Created:** [October 27, 2021, 1:32am UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272 "2021-10-27T01:32:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Carrascal](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/daniel_carrascal/32/8611_2.png) [@Daniel\_Carrascal](https://community.influxdata.com/u/Daniel_Carrascal)\
**Post date:** [October 27, 2021, 1:32am UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/1 "2021-10-27T01:32:35Z")

</div>

We are using grafana with flux. We are trying to show a sum of the field ‘peso’ while groping by other field called ‘viaje’.

![imagen_2021-10-27_032957](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/d/dc38a89182ac47848173110d0a33022f55b35c7c.png)

The measurement is like the dummy data of this image, where ‘domain’ is a tag and ‘id’, ‘peso’ and ‘viaje’ are fields.

I tried the following flux code but it does not group by anything.

```auto
    from(bucket: "hyperauth/autogen")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r._measurement == "metric" and r._field == "peso")
      |> group(columns: ["viaje"])
      |> yield()

```

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [October 27, 2021, 6:08am UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/2 "2021-10-27T06:08:02Z")

</div>

Hi. If you want to group by “viaje” you need it to be a tag, not a field.

And then group by “viaje”. And if you want to represent a sum of the field “peso” you also need to apply an aggregation function like |\> sum()

And also “id” you should also put it as a tag.

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrascal](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/daniel_carrascal/32/8611_2.png) [@Daniel\_Carrascal](https://community.influxdata.com/u/Daniel_Carrascal)\
**Post date:** [October 27, 2021, 12:41pm UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/3 "2021-10-27T12:41:22Z")

</div>

I read here that it was possible to group by a field with FLUX [https://github.com/influxdata/influxdb/issues/7200#issuecomment-426055525](https://github.com/influxdata/influxdb/issues/7200#issuecomment-426055525)

Edit:

> Hi. If you want to group by “viaje” you need it to be a tag, not a field.
> 
> And then group by “viaje”. And if you want to represent a sum of the field “peso” you also need to apply an aggregation function like |\> sum()
> 
> And also “id” you should also put it as a tag.

“Viaje” and “id” cannot be tags because of their high cardinality.

---

<div class="post-metadata">

**Author:** ![nathaniel](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nathaniel/32/9_2.png) [@nathaniel](https://community.influxdata.com/u/nathaniel)\
**Post date:** [October 27, 2021, 3:56pm UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/4 "2021-10-27T15:56:01Z")

</div>

You can group by fields but you need to get the field as column on the data first using the `schema.fieldsAsCols()` function

```auto
import "influxdata/influxdb/schema"

    from(bucket: "hyperauth/autogen")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => r._measurement == "metric" and ( r._field == "peso" or r._field == "viaje"))
      |> schema.fieldsAsCols()
      |> group(columns: ["viaje"])
      |> yield()

```

See [schema.fieldsAsCols() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/influxdata/influxdb/schema/fieldsascols/)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrascal](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/daniel_carrascal/32/8611_2.png) [@Daniel\_Carrascal](https://community.influxdata.com/u/Daniel_Carrascal)\
**Post date:** [October 27, 2021, 4:34pm UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/5 "2021-10-27T16:34:28Z")

</div>

Thank you so much!  
@nathaniel in Grafana where do I place the import?

Edit: I am using an older version of FLUX, I used

```auto
import "influxdata/influxdb/v1"

v1.fieldsAsCols()

```

---

<div class="post-metadata">

**Author:** ![nagashree](https://avatars.discourse-cdn.com/v4/letter/n/71e660/32.png) [@nagashree](https://community.influxdata.com/u/nagashree)\
**Post date:** [June 27, 2023, 5:57am UTC](https://community.influxdata.com/t/group-by-a-field-with-flux/22272/6 "2023-06-27T05:57:57Z")

</div>

Hi I am using the same above query but i am not able to find the solution.I am not able to group by fields
