Could you please try following SQL query with more data
Please try to create sample data for different processes as well
This query sums downtime grouped by process, you can remove process from aggregation SELECT statement (which is the last query) to calculate overall downtime. Or even add GroupId to the list for downtimes per chains of overlapping downtime periods
Please have a look at SQL tutorial on SQL Queries for Overlapping Time Periods which explains the solution in detail
;with rawdata as (
select
Process, id, StartTime, EndTime,
ROW_NUMBER() over (partition by Process order by StartTime, EndTime) as rn
from Processes
), cte as (
select
Process, StartTime, EndTime, rn, 1 as GroupId
from rawdata
where rn = 1
union all
select
p1.Process,
case
when (p1.starttime between p2.starttime and p2.endtime) then p2.starttime
when (p2.starttime between p1.starttime and p1.endtime) then p1.starttime
when (p1.starttime < p2.starttime and p1.endtime > p2.endtime) then p1.starttime
when (p1.starttime > p2.starttime and p1.endtime < p2.endtime) then p2.starttime
else p2.starttime
end as StartTime,
case
when (p1.EndTime between p2.starttime and p2.endtime) then p2.EndTime
when (p2.endtime between p1.starttime and p1.endtime) then p1.endtime
when (p1.starttime < p2.starttime and p1.endtime > p2.endtime) then p1.endtime
when (p1.starttime > p2.starttime and p1.endtime < p2.endtime) then p2.endtime
else p2.endtime
end as EndTime,
p2.rn,
case when
(p1.starttime between p2.starttime and p2.endtime) or
(p1.endtime between p2.starttime and p2.endtime) or
(p1.starttime < p2.starttime and p1.endtime > p2.endtime) or
(p1.starttime > p2.starttime and p1.endtime < p2.endtime)
then
p1.GroupId
else
(p1.GroupId+1)
end as GroupId
from cte p1
inner join rawdata p2
on p1.Process = p2.Process and
(p1.rn+1) = p2.rn
)
select
Process,
sum(datediff(second, StartTime, EndTime)) totalDownTime
from (
select
Process, GroupId, min(StartTime) StartTime, max(EndTime) EndTime
from cte
group by Process, GroupId
) t
group by Process
Output is as follows

Hoping to be useful,