# How to sum values from multiple host?

**URL:** <https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334>\
**Category:** Systems\
**Tags:** influxdb, grafana\
**Created:** [June 16, 2023, 4:56pm UTC](https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334 "2023-06-16T16:56:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sybux](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/sybux/32/13660_2.png) [@sybux](https://community.influxdata.com/u/sybux)\
**Post date:** [June 16, 2023, 4:56pm UTC](https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334/1 "2023-06-16T16:56:32Z")

</div>

Hello,  
I’m new to this new query model and I’m trying to sum 2 values in one.  
Here is my actual query :

> from(bucket: “monitoring”)  
> |\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
> |\> filter(fn: (r) =\> r[“\_measurement”] == “snmp”)  
> |\> filter(fn: (r) =\> r[“\_field”] == “L1”)  
> |\> filter(fn: (r) =\> r[“agent\_host”] == “h1” or r[“agent\_host”] == “h2”)

if L1 value for h1 is 12 and l1 value for h2 is 14, i’d like to get 26 as query result  
And to be complete, if I could divide this value by 10 it will be perfect.

---

<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:** [June 17, 2023, 3:04am UTC](https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334/2 "2023-06-17T03:04:21Z")

</div>

Welcome @sybux to the Influx forum.

You need to get the h1 and h2 on the same row in order to do a calculation involving both of them. To do this, use the `pivot()` function. Then you can use the `map()` function to do your desired calculation.

Here’s an example with my data which contains Outdoor Temperature and Humidity:

```auto
from(bucket: "Bucket1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "WeatherData")
  |> filter(fn: (r) => r["_field"] == "OutdoorTemp" or r["_field"] == "OutdoorHumidity")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "HumidityAndTemperature")

```

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

and with these two functions added:

```auto
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")  
  |> map(fn: (r) => ({ r with newColumn: r.OutdoorHumidity * r.OutdoorTemp }))  

```

…we get this new query:

```auto
from(bucket: "Bucket1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "WeatherData")
  |> filter(fn: (r) => r["_field"] == "OutdoorTemp" or r["_field"] == "OutdoorHumidity")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")  
  |> map(fn: (r) => ({ r with newColumn: r.OutdoorHumidity * r.OutdoorTemp }))  
  |> yield(name: "HumidityTimesTemperature")

```

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

---

<div class="post-metadata">

**Author:** ![sybux](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/sybux/32/13660_2.png) [@sybux](https://community.influxdata.com/u/sybux)\
**Post date:** [August 3, 2023, 3:37pm UTC](https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334/3 "2023-08-03T15:37:54Z")

</div>

Sorry for this long reply, to much work and welcome holidays 🙂

Thanks you for this example, it was perfect for me to do what I want. I have one last question, How can I change this query so it returns only the calculated field (HumidityTimesTemperature in your example).

---

<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:** [August 3, 2023, 10:59pm UTC](https://community.influxdata.com/t/how-to-sum-values-from-multiple-host/30334/4 "2023-08-03T22:59:48Z")

</div>

Glad you got the results you wanted. You can drop or keep columns using [drop](https://docs.influxdata.com/flux/v0.x/stdlib/universe/drop/) or [keep](https://docs.influxdata.com/flux/v0.x/stdlib/universe/keep/).
