# Combining data from two similar tables

**URL:** <https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036>\
**Category:** Fluxlang\
**Tags:** join, union\
**Created:** [March 22, 2021, 5:54pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036 "2021-03-22T17:54:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![christiaan](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/christiaan/32/7034_2.png) [@christiaan](https://community.influxdata.com/u/christiaan)\
**Post date:** [March 22, 2021, 5:54pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/1 "2021-03-22T17:54:17Z")

</div>

Feels like a straightfoward case, but I haven’t been able to manage the query.

I have two \_fields that contain values for two pumps that we’re monitoring. They both belong to the same station. I’m trying to make a query that provides a combined output table with three columns (for output to Grafana), like so:

```
Time Pump	Value
2021-03-22T14:14:06.340Z	Pump1	71.15999603271484	
2021-03-22T14:14:06.340Z	Pump2	164.20999145507812
2021-03-22T14:14:06.340Z	Pump1	73.70999145507812

```

or alternatively:

```
Time Pump1 Pump2
2021-03-22T14:14:06.340Z	71.15999603271484
2021-03-22T14:14:06.340Z 164.20999145507812
2021-03-22T14:14:06.340Z	73.70999145507812	

```

With separate queries I get the results for each pump just fine, but I can’t seem to combine them into a single table. When I do a union like below, I get both _separate_ tables in one view (and can switch between them in Grafana), but I don’t get a single table.

```
Pump1 = from(bucket: "StationData")
  |> range(start: -3h)
  |> filter(fn: (r) =>
    r._measurement == "plc" and
    r._field == "Pump1" and
    r.host == "Station1"
  )

Pump2 = from(bucket: "StationData")
  |> range(start: -3h)
  |> filter(fn: (r) =>
    r._measurement == "plc" and
    r._field == "Pump2" and
    r.host == "Station1"
  )

union(tables: [Pump1, Pump2])

```

Using ‘join’ I can get the results together in a single table, but, as one would expect in a join, I get a lot of duplicate fields

It feels like I want to do something like convert the field-names for Pump1 and Pump2 to both be ‘Pump’ and _then_ do the union. So I’ve been looking at the ‘Rename’ function, but without much success so far.

Anyone can give me a push in the right direction?

---

<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 23, 2021, 3:27pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/2 "2021-03-23T15:27:25Z")

</div>

@christiaan When you union two streams, it maintains the group keys, so the data is still partitioned into separate tables by series (common measurement, tag set, and field key).  
To group everything into a single table, you can just use `group()` (with no parameters).

In your particular use case, you don’t even need to create two streams and union them together. You can just do:

```javascript
from(bucket: "StationData")
  |> range(start: -3h)
  |> filter(fn: (r) =>
    r._measurement == "plc" and
    (r._field == "Pump1" or r._field == "Pump2") and
    r.host == "Station1"
  )
  |> group()

```

---

<div class="post-metadata">

**Author:** ![christiaan](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/christiaan/32/7034_2.png) [@christiaan](https://community.influxdata.com/u/christiaan)\
**Post date:** [March 23, 2021, 4:26pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/3 "2021-03-23T16:26:50Z")

</div>

@scott That did the trick! Figured it’d be something simple, but not quite this simple. Thanks a bunch!

---

<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 23, 2021, 4:37pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/4 "2021-03-23T16:37:57Z")

</div>

@christiaan No problem! Happy to help!

---

<div class="post-metadata">

**Author:** ![krutarth.joshi009](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@krutarth.joshi009](https://community.influxdata.com/u/krutarth.joshi009)\
**Post date:** [August 4, 2022, 12:02pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/5 "2022-08-04T12:02:18Z")

</div>

@scott  
I ran into same problem,  
The group is not combining two tables instead it remove all rows of second table.

Could you please help?

````auto
from(bucket: "STAGING")
	|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "zone")
  |> filter(fn: (r) => r["inst"] == "LEFRAK_SHORELINE")
  |> filter(fn: (r) => r["group"] == "RESIDENT_LOUNGE")
  |> filter(fn: (r) => r["zone"] == "LEFRAK_SHORELINE_WEST_LOUNGE_FULL_ROOM" or r["zone"] == "LEFRAK_SHORELINE_WEST_LOUNGE_GAME_ROOM_FULL_ROOM")
  |> filter(fn: (r) => r["cam"] == "LEFRAK_SHORELINE_WEST_LOUNGE" or r["cam"] == "LEFRAK_SHORELINE_WEST_LOUNGE_GAME_ROOM")
  |> filter(fn: (r) => r["_field"] == "count")
  |> group()```
````

---

<div class="post-metadata">

**Author:** ![krutarth.joshi009](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@krutarth.joshi009](https://community.influxdata.com/u/krutarth.joshi009)\
**Post date:** [August 4, 2022, 12:06pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/6 "2022-08-04T12:06:54Z")

</div>

so its working for small time window (max 15 mins), not combining if i change it to 1h.

---

<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:** [August 4, 2022, 2:34pm UTC](https://community.influxdata.com/t/combining-data-from-two-similar-tables/19036/7 "2022-08-04T14:34:22Z")

</div>

@krutarth.joshi009 What version of InfluxDB/Flux are you using? Can you provide any sample output?
