# Addition of queries via Expression option not working

**URL:** <https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256>\
**Category:** Grafana\
**Tags:** influxdb, query-help, dashboard, query-editor, templating\
**Created:** [January 18, 2024, 10:58am UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256 "2024-01-18T10:58:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [January 18, 2024, 10:58am UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/1 "2024-01-18T10:58:06Z")

</div>

Hey guys,

Using InfluxDB 1.8.10.  
I’m trying to monitor the bandwith of my routers. Since they don’t provide the total Rx Tx I have to calculate them by myself via query.

The goal is to calculate for each interface:  
the Rx and Tx from the past 24h to 12h and then the same from 12h to now(). Then subtract the last value to the first one.  
And, at the end, I should sum every result to get 24-hour bandwidth consumption.

I’ve tried (for the test I’m doing this under now() to 30m):

```auto
SELECT SUM("ifHCInOctets") as "Incoming_first"
FROM "interfaces"
WHERE 
  time >= now() - 30m
  AND time < now() - 15m
  AND agent_host =~ /^$14_router_IP$/
GROUP BY "ifDescr"

```

and

```auto
SELECT SUM("ifHCInOctets") as "Incoming_last"
FROM "interfaces"
WHERE 
  time >= now() - 15m
  AND time < now()
  AND agent_host =~ /^$14_router_IP$/
GROUP BY "ifDescr"

```

Which both returns values but when I add an expression to subtract my queries:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/d/6/d60b9be23452efa6718b3b640aa9a2b3fd8fa327.png)

with `number` unit, the data looks like this:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/d/b/db306c147b62a560429805de18468c693e01101b.png)

Also, by testing with numerical numbers, it’s working:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/4/9/49123360528ff072ad2b9d82b1f60e9321318ee8.png)

So I’m basically stuck at this point…

I’ve also tried the `transform data` option, but I got the same result.

If it can help, I can create a variable to get the interfaces name.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [January 18, 2024, 12:01pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/2 "2024-01-18T12:01:50Z")

</div>

What’s looks like a simple math (A+B) is not so simple.

Start simple math:

```auto
10 (result from query A) 
+ 
10 (result from query B) 
= 
20 (expected result from math expression $A+$B)

```

Actually, result from query is timeseries (see query config `Format as: Time series`) - there is list of values and each value represent some value in time, so it can be:

```auto
10 (result from query A at 2024-01-01 00:00:00) 
+
10 (result from query B at 2024-01-01 00:00:00) 
=
20 (expected result from math expression $A+$B at 2024-01-01 00:00:00)

```

But what if those values are from different times - what should be result of math expression - I would say this is undefined behaviour, because it is not possible to do a math:

```auto
10 (result from query A at 2023-01-01 00:00:00) 
+
10 (result from query B at 2024-01-01 00:00:00) 
=
???

```

There is link to Grafana doc, which is good to read:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/4/e4a4daad099e36975423f42fc887cf0bab7674b1.png)

> **[Write expression queries | Grafana documentation](https://grafana.com/docs/grafana/latest/panels-visualizations/query-transform-data/expression-queries/#math)**
>
> Write expression queries Server-side expressions enable you to manipulate data returned from queries with math and other operations. Expressions create new data and do not manipulate the data returned by data sources.
> About expressions Server-side...

> With binary operations, such as `$A + $B` or `$A || $B`, the operator is applied in the following ways depending on the type of data:
> 
> If both `$A` and `$B` are time series data, then the operation between each value in the two series is performed for each time stamp that exists in both `$A` and `$B`. The Resample operation can be used to line up time stamps. ( **Note:** in the future, we plan to add options to the Math operation for different behaviors).

I highlight it: If both `$A` and `$B` are time series data, then the operation between each value in the two series is performed _for each time stamp that exists in both_ `$A` and `$B`.

Editor has feature `Table view` feature:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/1/e17b2c22de65e95e5427267ec52432f707ba87e9.png)

Use it and inspect/debug time series for each query and make sure you have values with right time stamps, when you want to perform math.

But I would say just don’t generate time series and it will be simple math as you expect.

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [January 18, 2024, 12:44pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/3 "2024-01-18T12:44:19Z")

</div>

Once again that have been a very good help!

So I can’t actually do what I want to do since I need to “play” with time for the operation?

Any other idea to an alternative solution?

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [January 18, 2024, 12:54pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/4 "2024-01-18T12:54:35Z")

</div>

> But I would say just don’t generate time series and it will be simple math as you expect.

Just don’t use time series - I don’t see a reason why you need to format them as a timeseries:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/c/0/c04c24545332bbd7fb5d89be68ee2c1febbb9d88.png)

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [January 18, 2024, 1:02pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/5 "2024-01-18T13:02:14Z")

</div>

With Logs type I still get an Unknow value for my math expression and Table type returns me the erroe `[sse.readDataError] [A] got error: input data must be a wide series but got type long (input refid)`.  
By the way, I though we could only get the time of the collected data with Time Series…

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [January 18, 2024, 1:26pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/6 "2024-01-18T13:26:36Z")

</div>

Why log type? If you are only guessing, then please try also all other options there.

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [January 18, 2024, 1:52pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/7 "2024-01-18T13:52:01Z")

</div>

> [@jangaraj](#):
>
> If you are only guessing, then please try also all other options there.

Well, that’s what I’ve done, I only have three options:  
`Time series` → not working because of time  
`Table` → returns an error  
`Logs` → not working (and I guess because of time):

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/5/e59c0aa5bccd12a4a4c30f2034875dcf9cd6faaa.png)

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [January 18, 2024, 4:12pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/8 "2024-01-18T16:12:26Z")

</div>

> [@codi639](#):
>
> `Table` → returns an error

we cant remotely see your screen 😜 so can you share error

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [January 18, 2024, 4:24pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/9 "2024-01-18T16:24:01Z")

</div>

> [@codi639](#):
>
> With Logs type I still get an Unknow value for my math expression and Table type returns me the erroe `[sse.readDataError] [A] got error: input data must be a wide series but got type long (input refid)`.

Well, it was not clear as well I think haha.  
Table type returns me the error:

```auto
[sse.readDataError] [A] got error: input data must be a wide series but got type long (input refid)

```

With the query:

```auto
SELECT sum("ifHCInOctets") as "Incoming_first"
FROM "interfaces"
WHERE 
  time >= now() - 30m
  AND time < now() - 15m
  AND agent_host =~ /^$14_router_IP$/
GROUP BY "ifDescr"

```

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/8/b/8bdd5204746c79725084016e303677817ddeb22a.png)

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [January 20, 2024, 12:05am UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/10 "2024-01-20T00:05:48Z")

</div>

I would say you can’t generate non timeseries result with InfluxDB - there is still time dimension in the result.

You can try to use transformation - but don’t expect easy win. You will have to figure out how to use multiple set of transformations to get expected result.

I would say the easier option can be to generate timeseries with the same timestamp, so expression will be working fine (e.g. timegrouping with long time period).

The best option is if you change the approach. Total is just area under the curve for subsequent field values - and that’s a task for [INTEGRAL](https://docs.influxdata.com/influxdb/v1/query_language/functions/#integral). More complex InfluxQL (math function, maybe subquery), functions instead of simple A-B on the Grafana level.

BTW: I guess `ifHCInOctets` is a counter, so DERIVATE may be useful as well.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [January 20, 2024, 6:55am UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/11 "2024-01-20T06:55:58Z")

</div>

What happens if you remove the time filter from the where clause  
For test purposes  
Also what data type is time?

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [February 12, 2024, 9:47am UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/12 "2024-02-12T09:47:47Z")

</div>

Sorry for the delay, I went back to school…

Do you mean like this:

```auto
SELECT sum("ifHCInOctets") as "Incoming_first"
FROM "interfaces"
WHERE 
  agent_host =~ /^$14_router_IP$/
GROUP BY "ifDescr"

```

which returns me weird results as well:

![image](https://us1.discourse-cdn.com/grafana/original/3X/a/6/a6e483275ddaed7feb4548829b0b7c5e6534f94a.png)

I don’t understand what you mean by

> [@yosiasz](#):
>
> Also what data type is time?

I guess time is the Unix Time Stamp you can easily found on influxDB base:

```auto
time
----
1707730497000000000

```

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [February 14, 2024, 1:13pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/13 "2024-02-14T13:13:57Z")

</div>

After having tried numerous methods (including transformations) with no convincing results, I chose another solution. I created a script on my routers that sends the rx and tx of each interface to a php web server. The server takes care of the necessary calculations and sends everything back to a mysql database server.

But now I have a new problem:

The database looks like this:

```auto
+------------+------------+------------------------+------------+-----------+
| time | agent_host | intName | tx | rx |
+------------+------------+------------------------+------------+-----------+
| 1707911594 | 10.211.0.2 | LoopBr | 101335670 | 0 |

```

but this query:

```auto
SELECT sum(total_tx) 
FROM bandwidth 
WHERE agent_host =~ /^$routerIP$/
  AND time BETWEEN 
    (select MAX(time)
     from supervision.bandwidth) - 3000
    AND
    (select MAX(time)
     from supervision.bandwidth)

```

returns me the error:

```auto
db query error: Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '/^'10.211.0.2','10.211.0.3','10.211.0.4','10.211.0.5','10.211.0.6','10.211.0....' at line 3

```

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/5/e56a6927e2ebaf57742caaa22399f06711ea2c64.png)

I’ve also tried the query:

```auto
SELECT sum(total_tx) 
FROM bandwidth 
WHERE agent_host =~ $routerIP
  AND time BETWEEN 
    (select MAX(time)
     from supervision.bandwidth) - 3000
    AND
    (select MAX(time)
     from supervision.bandwidth)

```

Which returns the same erroe (without the " /^ ").

Also tried:

```auto
SELECT SUM(total_tx) 
FROM supervision.bandwidth 
WHERE agent_host =~ '${routerIP:singlequote}'
  AND time BETWEEN 
    (select MAX(time)
     from supervision.bandwidth) - 3000
    AND
    (select MAX(time)
     from supervision.bandwidth)

```

Which returns the same error

---

<div class="post-metadata">

**Author:** ![codi639](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@codi639](https://community.grafana.com/u/codi639)\
**Post date:** [February 14, 2024, 1:42pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/14 "2024-02-14T13:42:37Z")

</div>

I’m closing this topic as it seems to be changing subject. I’m reopening another one concerning my last message.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [February 14, 2024, 2:03pm UTC](https://community.grafana.com/t/addition-of-queries-via-expression-option-not-working/112256/15 "2024-02-14T14:03:17Z")

</div>

Closed on February 14, 2024, 2:03pm UTC


