with cte as ( select *, row_number() over (order by start_time) as rownum, DATEADD(MINUTE,-1* row_number() over (order by start_time) ,start_time) as updated_time from service_status where status = 'down' ) select service_name,min(start_time) as start_updated_time,max(start_time) as end_updated_time, status from cte group by service_name,updated_time,status having count(*)>3
with cte as (
select *, row_number() over (order by start_time) as rownum,
DATEADD(MINUTE,-1* row_number() over (order by start_time) ,start_time) as updated_time
from service_status where status = 'down'
)
select service_name,min(start_time) as start_updated_time,max(start_time) as end_updated_time,
status from cte group by service_name,updated_time,status
having count(*)>3