I'm trying to add 2 values in a single query, retrieved using the last()
operator.
In our setup, these two values can be selected by:
SELECT last("value_number") FROM "states_history" WHERE ("state_id" = '263083')
and
SELECT last("value_number") FROM "states_history" WHERE ("state_id" = '263084')
.So a quite simple setup, however, adding these two values within a single query seems impossible. The closest I got was using sub-queries:
SELECT (pkw1 + pkw2) AS pkw FROM
(SELECT last("value_number") AS pkw1 FROM "states_history" WHERE ("state_id" = '2630833') GROUP BY time(1d)),
(SELECT last("value_number") AS pkw2 FROM "states_history" WHERE ("state_id" = '2630834') GROUP BY time(1d))
I had to add the GROUP BY, otherwise Influx wouldn't accept the sub-querie. However, this still didn't lead to a usable result.
Could someone point me in the right direction? The addition of 2 scalar values (not time series) shouldn't be hard after all?
InfluxDB does not support joining subqueries directly. you can use a combination of JOIN
and GROUP BY
to achieve this. This is the simplified version . I think this will work.
SELECT sum("value_number") AS pkw FROM (
SELECT last("value_number") AS "value_number" FROM "states_history"
WHERE ("state_id" = '263083') OR ("state_id" = '263084')
GROUP BY "state_id"
)