# Calculating Runtime, Stop time & Breakdown time

**URL:** <https://forum.inductiveautomation.com/t/calculating-runtime-stop-time-breakdown-time/69220>\
**Category:** Ignition\
**Tags:** ignition81, namedqueries\
**Created:** [January 31, 2023, 5:06pm UTC](https://forum.inductiveautomation.com/t/calculating-runtime-stop-time-breakdown-time/69220 "2023-01-31T17:06:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNikte](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@SNikte](https://forum.inductiveautomation.com/u/SNikte)\
**Post date:** [January 31, 2023, 5:06pm UTC](https://forum.inductiveautomation.com/t/calculating-runtime-stop-time-breakdown-time/69220/1 "2023-01-31T17:06:47Z")

</div>

Hello,

I have been working on the machine downtime calculation project. How can I write a SQL server query to get Runtime, down time & breakdown time between a given pair of start and end timestamps.

In a transaction group, I am storing 3 tags RUN\_STS, STOP\_STS & BREAKDOWN\_STS. At any given time only 1 of these 3 tags is TRUE. So I have 000, 001, 010, 100 as system states. I want to calculate time elapsed in each state and store it in column(s), potentially using a named query. Please find the attached image for better explanation. Thanks in advance!

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/9/f9fd2faf80f5c0fe6790599e27f6bf6f24a96a45.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [January 31, 2023, 6:42pm UTC](https://forum.inductiveautomation.com/t/calculating-runtime-stop-time-breakdown-time/69220/2 "2023-01-31T18:42:25Z")

</div>

Welcome to the forum. Check out the thread below along with the other links in it and see if that helps. Essentially, you'l group by your states, and sum the timestamp difference between the current row and the previous row using a `lag()` window function most likely.

[Calculating Total Time in Seconds - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/calculating-total-time-in-seconds/49322/2)
