# Flux Join multiple series

**URL:** <https://community.influxdata.com/t/flux-join-multiple-series/20409>\
**Category:** InfluxDB 2\
**Tags:** influxdb, influxdata, query, flux, join\
**Created:** [June 16, 2021, 7:40pm UTC](https://community.influxdata.com/t/flux-join-multiple-series/20409 "2021-06-16T19:40:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rathodabhi0](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/rathodabhi0/32/6809_2.png) [@rathodabhi0](https://community.influxdata.com/u/rathodabhi0)\
**Post date:** [June 16, 2021, 7:40pm UTC](https://community.influxdata.com/t/flux-join-multiple-series/20409/1 "2021-06-16T19:40:31Z")

</div>

Hi I am have multiple vms reporting free disk space on each VM. Currently there are 4 VMs reporting this. To calculate total free disk space of all 4 VM I am able to write individual query assign those to seperate variables and then join all for of them.  
The issue is, if I want to add 5th VM or remove one VM. I have to modify the query. Is there a way to do this automate this part? I am writing the queries in Flux.

```auto
vm1 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm1”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")

vm2 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm2”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")

vm3 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm3”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")

vm4 = from(bucket: “<bucket-name>”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "memory")
|> filter(fn: (r) => r["_field"] == "memfree")
|> filter(fn: (r) => r["host"] == “vm4”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")

|> join(tables: {vm1:vm1, vm2:vm2, vm3:vm3, vm4:vm4}, on: ["_time", "_stop", "_start", “_field”, “_measurment”])
|> map(fn: (r) => ({
      _time: r._time,
      _field: r._field,
      _measurement: r._measurement	
      _value: r._value_vm1 + r._value_vm2 + r._value_vm3 + r._value_vm4
    })
  )

```

---

<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:** [June 21, 2021, 3:57pm UTC](https://community.influxdata.com/t/flux-join-multiple-series/20409/2 "2021-06-21T15:57:21Z")

</div>

Hello @rathodabhi0,  
Does that query run for you? I expect that you can only join two tables at one time. I can’t think of anyway to automate this query construction significantly.  
You could do something like:

```auto
myFunc = (tables=<-, host1=["_vm1"], host2=["_vm2"]) => {
  one = tables
    |> filter(fn: (r) => r["host"] == host1)
    |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  two = tables 
    |> filter(fn: (r) => r["host"] == host2)
    |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)

    myJoin = join(tables: {one:one, two:two}, on: ["_time", "_stop", "_start", “_field”, “_measurement”])
    return myJoin
}

```

But I don’t know if that’s really more helpful…
