# Calculate with Query

**URL:** <https://community.influxdata.com/t/calculate-with-query/8259>\
**Category:** Telegraf\
**Created:** [January 23, 2019, 2:29pm UTC](https://community.influxdata.com/t/calculate-with-query/8259 "2019-01-23T14:29:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![josvink66](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@josvink66](https://community.influxdata.com/u/josvink66)\
**Post date:** [January 23, 2019, 2:29pm UTC](https://community.influxdata.com/t/calculate-with-query/8259/1 "2019-01-23T14:29:01Z")

</div>

Hello,

I’m pretty new to influxDB and to SQL. So i’m not sure it’s possible what i’m trying to accomplish.  
I’ve setup a datapush from my Domoticz server to influxDB to create graphs in Grafana. This al works and I love the way it all looks.

But, Domoticz sends the total gas usage instead of the current use. What I want to do is show a Singlestat with the usage of this day. What I came up with is. I get the last value of yesterday and subtract that from the last value of today. That will give me todays usage.

So to test this I have 2 querys (doing the above but then with 4 hours apart):

**- 10 minutes ago:**

> curl -i -XPOST ‘http:/0.0.0.0:0000/query?pretty=true’ --data-urlencode “db=domoticz” --data-urlencode “q=SELECT last("value") AS sum\_lastNow FROM "Gas-usage" WHERE time \>= now()-10m LIMIT 1”

Result:  
“name”: “Gas-usage”,  
“columns”: [  
“time”,  
“sum\_lastNow”  
],  
“values”: [  
[  
“2019-01-23T14:15:48.764348885Z”,  
114.633  
]  
]

**4 hours ago:**

> curl -i -XPOST ‘http:/0.0.0.0:0000/query?pretty=true’ --data-urlencode “db=domoticz” --data-urlencode “q=SELECT first("value") AS sum\_lastYes FROM "Gas-usage" WHERE time \>= now()-4h LIMIT 1”

Result:  
“name”: “Gas-usage”,  
“columns”: [  
“time”,  
“sum\_lastYes”  
],  
“values”: [  
[  
“2019-01-23T10:23:11.87750021Z”,  
114.153  
]  
]

So that works. But now i want to combine the 2 and subtract them:

> curl -i -XPOST ‘http:/0.0.0.0:0000/query?pretty=true’ --data-urlencode “db=domoticz” --data-urlencode “q=SELECT "sum\_lastNow" - "sum\_lastYes" AS result FROM (SELECT last("value") AS sum\_lastYes FROM "Gas-usage" WHERE time \>= now()-4h LIMIT 1), (SELECT last("value") AS sum\_lastNow FROM "Gas-usage" WHERE time \>= now() -10m LIMIT 1)”

So this results:  
“name”: “Gas-usage”,  
“columns”: [  
“time”,  
“result”  
],  
“values”: [  
[  
“2019-01-23T14:22:38.550934512Z”,  
null  
],  
[  
“2019-01-23T14:22:38.550934512Z”,  
null  
]  
]

Maybe this isn’t even possible or i’m just doing it stupidly.  
Any advise would be welcome.

(i’ve changed the ip’s and ports and also the http part, got an error i can only post 2 links)

Greetings,  
Jos
