'Work with non-table values, aka "A is not subtractable"

I see many similar questions but couldn't find a good match. If we define a query and the result aught to be single value, is there a flux way to store as such? Example:

total = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> sum()

consumed = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> last()

total - consumed

Results in

invalid: error @18:1-18:40: [A] is not Subtractable

I can think of other ways to solve similar issues, but this example made me question whether flux actually supports easy working with single values or 1x1 relations.

Thanks



Solution 1:[1]

Not answering my original question but I want to provide the workaround I went with to solve this. I would still be interested in a more direct solution.

I've introduced a second column, then joined the two tables on that column:

total = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> sum()
    // Added:
  |> map(fn: (r) => ({ age: "latest", _value:r._value }))

consumed = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> last()
    // Added:
  |> map(fn: (r) => ({ age: "latest", _value:r._value }))

join(tables: {total: total, consumed: consumed}, on: ["age"])
  |> map(fn: (r) => ({_value: r._value_total - r._value_consumed}))

Solution 2:[2]

In the query, total and consumed are tables. For how to extract and use scalar values, please see Extract scalar values in Flux

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThomDietrich
Solution 2 alespour