My data is all in a bucket (energymeter), there are several measurements (IMPORTADA, EXPORTADA, PRECIO_EXPORTADO), and each measurement has a field that is the same for all measurements (potencia). Attached the complete query.
import "math"
importada = from(bucket: "energymeter")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "IMPORTADA")
|> aggregateWindow(
every: 1h,
fn: (tables=<-, column) =>
tables
|> integral(unit: 1h)
|> map(fn: (r) => ({ r with _value: r._value})), timeSrc: "_start")
|> keep(columns: ["_time", "_value"])
exportada = from(bucket: "energymeter")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "EXPORTADA")
|> aggregateWindow(
every: 1h,
fn: (tables=<-, column) =>
tables
|> integral(unit: 1h)
|> map(fn: (r) => ({ r with _value: r._value})), timeSrc: "_start")
|> keep(columns: ["_time", "_value"])
precioexportado = from(bucket: "energymeter")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "PRECIO_EXPORTADO")
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false, timeSrc: "_start")
|> keep(columns: ["_time", "_value"])
kWhresultantes = join(tables: {key1: importada, key2: exportada}, on: ["_time"], method: "inner")
|> map(fn: (r) => ({
_time: r._time,
_value: ((r._value_key1 - r._value_key2)),
}))
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false, timeSrc: "_start")
|> keep(columns: ["_time", "_value"])
preciokWhexportados = join(tables: {key3: kWhresultantes, key4: precioexportado}, on: ["_time"], method: "inner")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> map(fn: (r) => ({
_time: r._time,
_value: (((r._value_key3 - math.abs(x: r._value_key3))/(2.0 * r._value_key3)) * r._value_key3 * r._value_key4 )
}))
|> sum(column: "_value")
|> yield(name: "preciokWhexportados")
kWhexportados = join(tables: {key5: kWhresultantes, key6: precioexportado}, on: ["_time"], method: "inner")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> map(fn: (r) => ({
_time: r._time,
_value: (((r._value_key5 - math.abs(x: r._value_key5))/(2.0 * r._value_key5)) * r._value_key5 )
}))
|> sum(column: "_value")
|> yield(name: "kWhexportados")
join(tables: {key7: preciokWhexportados, key8: kWhexportados}, on: ["_measurement"], method: "inner")
|> map(fn: (r) => ({ r with prueba: r._value_key7 / r._value_key8 }))
|> yield()
The last join, if I cancel the two previous yield(), does not give results (NO RESULTS)