# Two fields in two tables to be subtracted and put in a new field

**URL:** <https://community.influxdata.com/t/two-fields-in-two-tables-to-be-subtracted-and-put-in-a-new-field/14584>\
**Category:** Systems\
**Tags:** influxdb\
**Created:** [May 30, 2020, 1:52pm UTC](https://community.influxdata.com/t/two-fields-in-two-tables-to-be-subtracted-and-put-in-a-new-field/14584 "2020-05-30T13:52:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sorriso93](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@sorriso93](https://community.influxdata.com/u/sorriso93)\
**Post date:** [May 30, 2020, 1:52pm UTC](https://community.influxdata.com/t/two-fields-in-two-tables-to-be-subtracted-and-put-in-a-new-field/14584/1 "2020-05-30T13:52:10Z")

</div>

Hello, I’m trying to obtain a new field that is the subtraction of two fields in two different tables. Tha syntax below is ok but gives no result, whereas the single queries get the right result.  
Any suggestion what is wrong?  
Many thanks

SELECT (non\_negative\_derivative(mean(“POWERPANNELLI”), 120s )- non\_negative\_derivative(mean(“POWERCASA”), 120s ))

AS “Consumo”

FROM (

SELECT mean(“ENERGY\_Power”) AS “POWERCASA” FROM “telegraf”.“autogen”.“CASA” WHERE time \> :dashboardTime: AND time \< :upperDashboardTime: GROUP BY time(1m)

), (

SELECT mean(“ENERGY\_Power”) AS “POWERPANNELLI” FROM “telegraf”.“autogen”.“Pannelli” WHERE time \> :dashboardTime: AND time \< :upperDashboardTime: GROUP BY time(1m)

)

GROUP BY time(1m) fill(null)

---

<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 1, 2020, 2:51pm UTC](https://community.influxdata.com/t/two-fields-in-two-tables-to-be-subtracted-and-put-in-a-new-field/14584/2 "2020-06-01T14:51:17Z")

</div>

Hello @sorriso93,  
You can’t perform math across measurements with Influxql. You need to use flux to do that. You could run a task and store the new values in a different measurement. You can [enable flux in 1.7+](https://docs.influxdata.com/flux/v0.65/introduction/getting-started/).  
Please take a look at this documentation:

> **[join() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/join/)**
>
> join() merges two streams of tables into a single output stream based on columns with equal values. Null values are not considered equal when comparing column values. The resulting schema is the union of the input schemas. The resulting group key is...

And this blog:

> **[InfluxDB: How to Do Joins, Math across Measurements | Blog | InfluxData](https://www.influxdata.com/blog/influxdb-how-to-do-joins-math-across-measurements/)**
>
> InfluxData has released the technical preview of Flux, and with it comes the ability to perform math across measurements.
