Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database with the following format that includes machine operational status:

Event_Start,Event_End,Duration,Status
06/23/2018 07:00:00 AM,06/23/2018 07:01:00 AM,60,1
06/23/2018 07:01:00 AM,06/23/2018 07:10:00 AM,540,2
06/23/2018 07:10:00 AM,06/23/2018 07:20:00 AM,600,1
06/23/2018 07:20:00 AM,06/23/2018 07:25:00 AM,300,2
06/23/2018 07:25:00 AM,06/23/2018 07:27:00 AM,120,1
06/23/2018 07:27:00 AM,06/23/2018 07:28:00 AM,60,1
06/23/2018 07:28:00 AM,06/23/2018 07:31:00 AM,180,3
06/23/2018 07:31:00 AM,06/23/2018 08:00:00 AM,1740,2


What I am trying to do is to find blocks of time where the machine is not running for 5 or minutes (duration of 300 or more). The machine is only running when the status is set to 2. These blocks of times can consist of a single row or multiple adjacent rows. The key to this is only summing the adjacent rows that are not status 2. For example:

The first row in the data is not status 2 and is for 60 seconds. The next row is status 2 so I need to stop totaling. Since 60 seconds is under my 300 second threshold, I don’t want to report this.

The 3rd row contains an entry where the machine is not status 2. The 600 seconds is over my 300 seconds threshold so I need to report this. I also need to look for more adjacent rows but since the 4th row is status 2, I need to stop totaling and just report the 600 seconds.

The 5th row contains an entry where the machine is not status 2. The 120 seconds is under my 300 second threshold but I still need to look for more adjacent rows. The 6th row is also not status 2 so I can add the 60 seconds to the 120 seconds. The 7th row is also not status 2 so I can add the 180 seconds so I can add this to the other 2 entries. Lastly, the 8th rows is status 2 so I need to stop totaling. If I total up the 3 rows (60 + 120 + 180) = 360. This is greater than my 300 second threshold so I need to report this.

When done, I should have 2 rows reported in my results. The 2nd row of my results should include the start time of the 5th row and the end time of the 7th row. Lastly, the duration should include the sum of seconds from 7:25 to 7:31.

Event_Start,Event_End,Duration
06/23/2018 07:10:00 AM,06/23/2018 07:20:00 AM,600
06/23/2018 07:25:00 AM,06/23/2018 07:31:00 AM,360

What I have tried:

I started out with the following SQL but it only gives individual rows greater than 300 seconds. I read a little about using the LEAD command to look at the next row but I don’t know how I can make it where it will keep summing down to the next row that is not status 2.

select event_start, event_end, duration
from mazak_oee_log
where event_start >= '06/23/2018'
and event_end <= '06/24/2018'
and status <> 2
and duration >= 300
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900