На выходе в поле tsrange имею два временных отрезка:
["2010-01-01 09:00:00","2010-01-01 09:30:00")
["2010-01-01 10:30:00","2010-01-01 18:00:00")
Запрос таков:
WITH mins(lt) AS (
/* get all lower bounds of intervals */
SELECT lower(during)
FROM reservation
UNION
/* get 18:00 for each day */
SELECT date_trunc('day', lower(during)) + INTERVAL '18 hours'
FROM reservation
), maxs(ut) AS (
/* get all upper bounds of intervals */
SELECT upper(during)
FROM reservation
UNION
/* get 09:00 for each day */
SELECT date_trunc('day', lower(during)) + INTERVAL '9 hours'
FROM reservation
)
SELECT tsrange(ut, lt) /* candidate for a free interval */
FROM mins
JOIN maxs
ON date_trunc('day', lt) = date_trunc('day', ut)
AND ut < lt
/* exclude all such intervals that overlap an entry*/
WHERE NOT EXISTS (SELECT 1 FROM reservation
WHERE during && tsrange(ut, lt, '()'))
ORDER BY ut;
Таблица такая:
CREATE TABLE reservation (room int, during tsrange);
INSERT INTO reservation VALUES
(1108, '[2010-01-01 09:30, 2010-01-01 10:30)');
Вопрос, ребята прошу помочь с разделением этих отрезков на 15 минутные отрезки. Заранее благодарю.