Calculating between 2 timestamps


I’m trying to configure the dashboard so when i select the time range, a bar gauge will show the difference of the value between the timestamps. I’m am using a MS SQL database as datastorage. I already tried follow query:

(SELECT Verbruik FROM GalvanoLijnen3 WHERE TimeStamp <= $__timeTo()::timestamp ORDER BY TimeStamp DESC LIMIT 1) -
(SELECT Verbruik FROM GalvanoLijnen3 WHERE TimeStamp >= $__timeFrom()::timestamp ORDER BY TimeStamp ASC LIMIT 1) AS VerbruikDifference
, but that gave an error. I’m wondering if i’m doing something wrong with the query, or if i should try to get the value by using the transformation tab.

Left image a visualisation and for example the gauge should then show the difference of value between 10 and 10:35.
The current code i’m using for the gauge is:
SELECT TimeStamp AS “time”, Verbruik FROM Monitoring.dbo.GalvanoLijnen3


What error was it? Also are you always returning only 1 value per query filter?

Error message is as following: Status: 500. Message: db query error: sql: expected 1 arguments, got 0.
And yes for the filter only returns 1 value.
Kind regards

Try using variables @min and @max ,assign these to the corresponding queries
Then do select @max - @min

for example

1 Like

Thank you, i’ll try this and let you know if it works.

Hello i was wondering if you have an example like this but if the format is Time series?

is Verbruik timestamp?

No “Verbruik” is the data saved with a TimeStamp. I’m to trying to program the query so when you select specific timestamp in Grafana, it will display the difference from Verbruik between those 2 timestamps

so what data type is Verbruik?
Also was your original question answered by

Verbruik is saved as a float, and sort off. We weren’t able to use it in this specific case. But it helped to visualise another project :slight_smile:

So your latest question is about a timestamp and not Verbruik the float data type? This seems to be a totally new question unrelated to the original question which was about subtracting 2 Verbruik values from 2 different time stamps

I had incorrectly asked my last question. After some figuring out, I managed to visualise the value with the piece of code you had shown.
Thanks for the help.

1 Like