I presume you want to ignore repeat ones and zeros in the output column? If so, running periods from table1 would be something like this:
Select t_stamp as start_ts,
lead(t_stamp, 1, current_timestamp) Over (Order by t_stamp) as end_ts
From (
Select t_stamp, output
From (
Select t_stamp,
output,
lag(output, 1, 0) Over (Order By t_stamp) as lag_out
From table1
) innerq
Where output != lag_out
) changesq
Where output = 1
Order By start_ts
And similarly, downtime periods from table2 would be something like this:
Select t_stamp as start_ts,
lead(t_stamp, 1, current_timestamp) Over (Order by t_stamp) as end_ts
From (
Select t_stamp, output
From (
Select t_stamp,
output,
lag(output, 1, 1) Over (Order By t_stamp) as lag_out
From table2
) innerq
Where output != lag_out
) changesq
Where output = 0
Order By start_ts
You would make a join of these two queries where the time spans overlap.
In each of those, innerq
obtains the previous record’s output
value and then changesq
throws away the repeat rows. The outermost Select
then produces time spans and throws away the intervening opposite states.