I am struggling on how to craft a query to figure out my machine uptime.
I have a system which logs machine state every 10 seconds. If the machine is running, it’ll be a 1, otherwise it’ll be a zero.
The problem is, how do I count times where there are no entries? The OPC server that is feeding the SQL server just won’t log any entry if the machine is offline. Obviously there won’t be any entries either if the SQL server is down.
For accurate data and uptime percentages, I want to count these “missing rows” (records at 10 second intervals that don’t actually exist) as 0’s.
I have a pi chart with the following query which accurately counts the 1’s & 0’s but the machine was turned completely off for several hours, so the ratio is way off.
SELECT
0 As time, /*Create a column named 'time' filled with zeros, grafana REALLY wants a time column.*/
count(IS_RUNNING) as 'Counts', /*Create a column called 'Counts' that holds the sample count of each state.*/
cast(IS_RUNNING as nvarchar(max)) as 'State' /*Create a column called 'State' that displays what value each count is.*/
FROM
AV_01_LIVE /*Data table selection.*/
WHERE
TIME BETWEEN '2022-08-11T10:24:59Z' AND '2022-08-11T13:24:59Z' /*Filter by time selection.*/
GROUP BY IS_RUNNING /*Command to group the data appropriately.*/
Any help on how to alter this? Thank you.