Попробуйте:
WITH FOO (Id_timeseries, number, timestamp) AS
(
VALUES
(1, 28, timestamp('2017-01-01 01:00:00'))
, (1, 28, timestamp('2017-01-01 02:00:00'))
, (1, 28, timestamp('2017-01-01 03:00:00'))
, (1, 28, timestamp('2017-01-01 06:00:00'))
, (1, 28, timestamp('2017-01-01 07:00:00'))
--, (1, 28, timestamp('2017-01-01 00:00:00'))
--, (1, 28, timestamp('2017-01-01 23:00:00'))
--, (1, 28, timestamp('2017-01-02 00:00:00'))
, (2, 28, timestamp('2017-01-01 01:00:00'))
, (2, 28, timestamp('2017-01-01 02:00:00'))
, (2, 28, timestamp('2017-01-01 03:00:00'))
)
-- Internal gaps
SELECT Id_timeseries, timestamp_prev + 1 hour as from, timestamp - 1 hour as to
FROM
(
SELECT Id_timeseries, timestamp, lag(timestamp) over (partition by Id_timeseries order by timestamp) timestamp_prev
FROM FOO
)
WHERE timestamp_prev <> timestamp - 1 hour
-- Start gap
UNION ALL
SELECT Id_timeseries, timestamp('2017-01-01 00:00:00') as from, min(timestamp) - 1 hour as to
FROM FOO
GROUP BY Id_timeseries
HAVING timestamp('2017-01-01 00:00:00') <> min(timestamp)
-- End gap
UNION ALL
SELECT Id_timeseries, max(timestamp) + 1 hour as from, timestamp('2017-01-02 00:00:00') as to
FROM FOO
GROUP BY Id_timeseries
HAVING timestamp('2017-01-02 00:00:00') <> max(timestamp)
ORDER BY Id_timeseries, from;
Результат:
|ID_TIMESERIES|FROM |TO |
|-------------|--------------------------|--------------------------|
|1 |2017-01-01-00.00.00.000000|2017-01-01-00.00.00.000000|
|1 |2017-01-01-04.00.00.000000|2017-01-01-05.00.00.000000|
|1 |2017-01-01-08.00.00.000000|2017-01-02-00.00.00.000000|
|2 |2017-01-01-00.00.00.000000|2017-01-01-00.00.00.000000|
|2 |2017-01-01-04.00.00.000000|2017-01-02-00.00.00.000000|