assuming that between from_time and to_time can pass more than one hour and that more names are booked for the same room in the same hour, try to use the pivot operator with the addition of the with
construct in the following way.
You have to know all the possible values for room
.
ID DATE ROOM OWNER FROM_TIME TO_TIME
1 jan-1-18 Room_A Jack 09:00 10:00
2 jan-1-18 Room_A John 10:00 11:00
3 jan-1-18 Room_B John 09:00 10:00
4 jan-1-18 Room_A Jill 09:00 11:00
I added the "Jill" case that occupies room A from 09:00 to 11:00: the query shows the two hours separately:
with data as
(select level-1 l from dual connect by level <= 100),
dtset as
(select id_row, day, room, owner,
to_char(to_date(from_time, 'hh24:mi') + (l/24), 'hh24:mi') from_time,
to_char(to_date(from_time, 'hh24:mi') + ((l+1)/24), 'hh24:mi') to_time
from your_table_name, data
where l < (24 * (to_date(to_time, 'hh24:mi') - to_date(from_time, 'hh24:mi'))))
select *
from (select day, from_time, to_time, room, owner
from dtset)
pivot (listagg(owner, ', ') within group (order by owner) for room in ('Room_A' as Room_A, 'Room_B' as Room_B))
order by day, to_time;
in short, materialized dtset
divides the two-hour time slot into TWO one-hour slots. Result:
DATA FROM_TIME TO_TIME ROOM_A ROOM_B
---------- --------- -------- ---------- -------
11/01/2018 09:00 10:00 Jack, Jill John
11/01/2018 10:00 11:00 Jill, John