CREATE TABLE reservation (room int, during tsrange);
INSERT INTO reservation VALUES
(1108, '[2010-01-01 09:30, 2010-01-01 10:30)');
30 мин
1. 09.00 - 09.30
2. 10.30 - 11.00
3. 11.30. -12.00
....
45 мин
1. 10.30 - 11.15
2. 11.15 - 12.00
...
1 час
1. 10.30. - 11.30
2. 11.30 - 12.30
....
with
all_ranges as (
SELECT tsrange(generate_series,
generate_series + interval '30' minute,
'()') as range
FROM generate_series(current_date::timestamp + interval '9' hour,
current_date::timestamp + interval '18' hour,
'30 minutes'))
select rooms.id as room_id,
concat(to_char(lower(ar.range), 'HH24:MI'),
' - ',
to_char(upper(ar.range), 'HH24:MI')) as timerange
from all_ranges as ar
cross join rooms
where not exists (select 1
from reservations as rt
where rooms.id=rt.room_id and
ar.range && rt.occurrence)
order by 1;