This query returns me the list of room #077 that is occupied on specific day; how do I reverse this query and show only times that are NOT in the database between 07:00:00 and 22:00:00? (30 minutes intervals) and each class should take only 1 hour and 30 minutes
select
*
from
(select
rooms.id, rooms.number, rooms.building, rooms.capacity
from
rooms) R1,
(select
exam_schedules.room_id,
exam_schedules.day,
exam_schedules.start_time,
exam_schedules.end_time
from
exam_schedules) R2
where
R2.room_id = R1.id and R2.day = 'tuesday' and R1.number = '077'
This is the result:
ID number Bulding Capacity room_id day start_time end_time
1 077 ACT 12 1 tuesday 10:30:00 12:00:00
But I need the result that is shown below (which is pretty much shows the AVAILABLE times that are NOT occupied by exams AND could no be occupied by other exam since it might cause scheduling issues: for example if exam is already scheduled at 10.30, only 9.00 (not 9.30) should be shown since if 9.30 is shown – it will cause the conflict: 9.30+1.30 = 11.00 – but I already have the 10.30-12.00 scheduled for this room)
1 077 ACT 12 1 tuesday 07:00:00
1 077 ACT 12 1 tuesday 07:30:00
1 077 ACT 12 1 tuesday 08:00:00
1 077 ACT 12 1 tuesday 08:30:00
1 077 ACT 12 1 tuesday 09:00:00
//note that time frame from 9.30-10.30 is not available since there is a class at 10.30 scheduled already
//note that time period 10.30-<12.00 not shown since class is already scheduled for this timeframe 10.30-12.00
1 077 ACT 12 1 tuesday 12:00:00
1 077 ACT 12 1 tuesday 12:30:00
1 077 ACT 12 1 tuesday 13:00:00
1 077 ACT 12 1 tuesday 13:30:00
1 077 ACT 12 1 tuesday 14:00:00
1 077 ACT 12 1 tuesday 14:30:00
1 077 ACT 12 1 tuesday 15:00:00
1 077 ACT 12 1 tuesday 15:30:00
1 077 ACT 12 1 tuesday 16:00:00
1 077 ACT 12 1 tuesday 16:30:00
1 077 ACT 12 1 tuesday 17:00:00
1 077 ACT 12 1 tuesday 17:30:00
1 077 ACT 12 1 tuesday 18:00:00
1 077 ACT 12 1 tuesday 18:30:00
1 077 ACT 12 1 tuesday 19:00:00
1 077 ACT 12 1 tuesday 19:30:00
1 077 ACT 12 1 tuesday 20:00:00
1 077 ACT 12 1 tuesday 20:30:00
1 077 ACT 12 1 tuesday 21:00:00
1 077 ACT 12 1 tuesday 21:30:00
1 077 ACT 12 1 tuesday 22:00:00
The table with all possible timeframes does not exist in the database. Can I maybe hadcode it into the query?
You need to create the list of all possible values. You can do this with a query like:
select *
from (select ‘2012-01-01 7:00:00’ from dual union all
select ‘2012-01-01 7:30:00’ from dual union all . . .
) times
That is, you can use union all to put the data together. I find it easiest to use Excel to put together the SQL statements needed to make this work.
That said, a calendar/calendar-time table is something you should consider. Having a table with each possible time slot it in will probably help you in the future.