Calculate the time differance between all TimeDate values and chart

Grafana version 9.4.3

I have a query that returns two columns, a number and a time stamp. I would like to show instead calculate the time difference between the values, so this can be displayed as a bar chart highlighting the variation between each time stamp

SELECT TimeDate, Step as value FROM dbo.AGVstep 
WHERE $__timeFilter(TimeDate) AND Program <> 0 AND Program in ($Program) and Step in ($Step) and AGV in ($AGV)
order by TimeDate asc

I would achieve this in excel by simply using each value and subtracting the previous

Can anyone advise how I can do this in Grafana?

Thanks

This is more of a sql query question than it is a grafana question.

That said what type of server is this? mysql, postgres?

mysql

Check out this function, try it out and if stuck let us know

1 Like

Thanks for the help with the LAG function.

SELECT TimeDate, 
       (DATEDIFF(SECOND, TimeDate, lag(TimeDate, 1) 
       OVER (ORDER BY TimeDate desc))) as CycleTime
  FROM AGVstep
  WHERE $__timeFilter(TimeDate) 
    AND Program <> 0 
    AND Program in ($Program) 
    AND Step in ($Step) and AGV in ($AGV)
  order by TimeDate asc

Works as I hoped it would.

2 Likes