Help using flux sum()

Hello,

I’m running influxdb 1.7.6 .

In influx I’m doing:

> select sum(EUR) from profit where time > now() - 1d;
name: profit
time                sum
----                ---
1559643752830530723 -2.9350230889663074

Working fine. Now in flux I’m doing:

from(bucket: "money")
|> range(start: -1d)
|> filter(fn: (r) => r._measurement == "profit" and r._field == "EUR")
|> sum()

This doesn’t give me the sum, but a all entries in the table money from 1 day ago until now. Including all columns, not only “EUR”. What am I doing wrong ?

Thanks

So… this happens when the tags are not the same.

influxql:

> select * from test;
name: test
time                tag1 tag2 val1 val2
----                ---- ---- ---- ----
1559739619893678416 a    b    10   20
1559746780130967615 c    d    100  200
1559747185291002660 a    b    1    2
1559747196858276080 a    d    1000 2000
> select sum(val1) from test;
name: test
time sum
---- ---
0    1111

Flux seems to by default group by all unique tag combinations. It’s summing all values where tag1 and tag2 are the same. If they are not the same, they get summed separately.

Connected to http://localhost4:8086 version 1.7.6
InfluxDB shell version: 1.7.6
Enter a Flux query
> from(bucket: "test")
|> range(start: -1y)
|> filter(fn: (r) => r._measurement == "test" and r._field == "val1")
> from(bucket: "test")
|> range(start: -1y)
|> filter(fn: (r) => r._measurement == "test" and r._field == "val1")
|> sum()
Result: _result
Table: keys: [_start, _stop, _field, _measurement, tag1, tag2]
                   _start:time                      _stop:time           _field:string     _measurement:string             tag1:string             tag2:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2018-06-06T15:07:49.611260047Z  2019-06-05T15:07:49.611260047Z                    val1                    test                       a                       b                            11
Table: keys: [_start, _stop, _field, _measurement, tag1, tag2]
                   _start:time                      _stop:time           _field:string     _measurement:string             tag1:string             tag2:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2018-06-06T15:07:49.611260047Z  2019-06-05T15:07:49.611260047Z                    val1                    test                       a                       d                          1000
Table: keys: [_start, _stop, _field, _measurement, tag1, tag2]
                   _start:time                      _stop:time           _field:string     _measurement:string             tag1:string             tag2:string                  _value:float
------------------------------  ------------------------------  ----------------------  ----------------------  ----------------------  ----------------------  ----------------------------
2018-06-06T15:07:49.611260047Z  2019-06-05T15:07:49.611260047Z                    val1                    test                       c                       d                           100
>

How can I make flux to not group by all unique tag combinations ?

To make sure that flux commands dont group based on tags, you can follow one of 2 things mentioned below

  1. Use keep()
    from(bucket: “test”)
    |> range(start: -1y)
    |> filter(fn: ® => r._measurement == “test” and r._field == “val1”)
    |>keep(columns:["_value"])
    |> sum()

  2. You can design your database in such a way that there are no tags.
    But this would make filter queries very inefficient