# Min Max by day on multiple measurements

**URL:** <https://community.influxdata.com/t/min-max-by-day-on-multiple-measurements/26675>\
**Category:** InfluxDB 2\
**Created:** [September 22, 2022, 2:07pm UTC](https://community.influxdata.com/t/min-max-by-day-on-multiple-measurements/26675 "2022-09-22T14:07:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Franck\_Bellot](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franck_bellot/32/10162_2.png) [@Franck\_Bellot](https://community.influxdata.com/u/Franck_Bellot)\
**Post date:** [September 22, 2022, 2:07pm UTC](https://community.influxdata.com/t/min-max-by-day-on-multiple-measurements/26675/1 "2022-09-22T14:07:42Z")

</div>

Hello,

I would like to get by day the min and max for each selected measurement.

below the selection query  
How can I get by day the min and the max for each measurement BBRHCJC … int the same query

Thanks in advance for your support on it

```auto
data = from(bucket: "datainfo")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "BBRHCJB" or r["_measurement"] == "BBRHCJR" or r["_measurement"] == "BBRHCJW" or r["_measurement"] == "BBRHPJB" or r["_measurement"] == "BBRHPJR" or r["_measurement"] == "BBRHPJW" or r["_measurement"] == "HCHC" or r["_measurement"] == "HCHP")
  |> filter(fn: (r) => r["_field"] == "Compteur")
  |> aggregateWindow(every: 1d, fn: max, createEmpty: false)

```

---

<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:** [September 22, 2022, 5:03pm UTC](https://community.influxdata.com/t/min-max-by-day-on-multiple-measurements/26675/2 "2022-09-22T17:03:47Z")

</div>

@Franck_Bellot You can calculate the min and max in separate streams and then union them together:

```javascript
data = () =>
    from(bucket: "datainfo")
        |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
        |> filter(
            fn: (r) =>
                r["_measurement"] == "BBRHCJB" or r["_measurement"] == "BBRHCJR" or r["_measurement"] == "BBRHCJW"
                    or
                    r["_measurement"] == "BBRHPJB" or r["_measurement"] == "BBRHPJR" or r["_measurement"] == "BBRHPJW"
                    or
                    r["_measurement"] == "HCHC" or r["_measurement"] == "HCHP",
        )
        |> filter(fn: (r) => r["_field"] == "Compteur")

min =
    data()
        |> aggregateWindow(every: 1d, fn: min, createEmpty: false)
        |> set(key: "_field", value: "min_Compteur")

max =
    data()
        |> aggregateWindow(every: 1d, fn: max, createEmpty: false)
        |> set(key: "_field", value: "max_Compteur")

union(tables: [min, max])

```

A few notes:

- I wrote `data()` as a function to preserve the pushdown through to `aggregateWindow()`, just to make sure the query is as performant as possible.
- I renamed the field in each stream so you know which represent the min and max.

---

<div class="post-metadata">

**Author:** ![Franck\_Bellot](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/franck_bellot/32/10162_2.png) [@Franck\_Bellot](https://community.influxdata.com/u/Franck_Bellot)\
**Post date:** [September 22, 2022, 9:45pm UTC](https://community.influxdata.com/t/min-max-by-day-on-multiple-measurements/26675/3 "2022-09-22T21:45:25Z")

</div>

Thanks a lot, it’s helping me a lot.  
I do not understand the added value of the data() as function, that’s the first time than I see this kind of ‘step’. I will take time during week-end to get info about it 😉

One added question  
In the future, I will need to use the result, to calculate the gap value between min and max.  
I have added the line pivot on measurement result by date  
pivot(rowKey:[“\_time”], columnKey: [“\_measurement”], valueColumn: “\_value”)

How can I group min and max value of all the measurements by date to be able in the future to calculate the difference/gap between min and max using map() function.

Thanks again for your support

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