I have an appointments table with the following info:
id
agent_id
starts_at
ends_at
...
and given an agents schedule starts at 9:00AM and ends at 6:00PM (18:00)
and given an appointment scheduled between 10:00AM and 11:00AM on 2011-01-01
would it be possible to divide his schedule in multiples of 30 minutes
and get the following result using SQL in Oracle:
date, slotnum, agent_id, starts_at, ends_at, appointment_id
2011-01-01, 1, 1, 09:00, 09:30, (null)
2011-01-01, 2, 1, 09:30, 10:00, (null)
2011-01-01, 3, 1, 10:00, 10:30, 1
2011-01-01, 4, 1, 10:30, 11:00, 1
2011-01-01, 5, 1, 11:00, 11:30, (null)
...
...
2011-01-01,, 18, 1, 17:30, 18:00, (null)
Thanks in advance.
I learned a couple of things myself while making this work. Hopefully this gives you an idea of one way of doing this. My example has a record in each table, so with more records, you may need to tweak it for performance...
create table t_agent
(id number(9),
agent_id number(9),
starts_at timestamp,
ends_at timestamp);
insert into t_agent values (1,100, timestamp'2011-01-01 09:00:00.0 AMERICA/CHICAGO',timestamp'2011-01-01 18:00:00.0 AMERICA/CHICAGO');
create table t_appointment
(id number(9),
agent_id number(9),
start_time timestamp,
end_time timestamp);
insert into t_appointment values (1,100, timestamp'2011-01-01 10:00:00.0 AMERICA/CHICAGO',timestamp'2011-01-01 11:00:00.0 AMERICA/CHICAGO');
with agent_hours as
(select extract(hour from ends_at) end_time,
extract(hour from starts_at) start_time,
to_char(trunc(starts_at)) appointment_date,
starts_at,
ends_at,
agent_id
from t_agent
where agent_id=100)
select slots.appointment_date,
slots.slotnum,
slots.starttime,
slots.endtime,
case when app.start_time >= slots.full_starttime and app.start_time < slots.full_endtime
or
app.end_time > slots.full_starttime and app.end_time <= slots.full_endtime
then app.id else null end app_id
from t_appointment app,
(select agent_hours.appointment_date,
rownum slotnum,
agent_hours.agent_id,
to_char((trunc(starts_at) + (.5/24) * agent_hours.start_time * 2) + ((rownum -1) * .5/24),' HH24:mi') starttime,
to_char((trunc(starts_at) + (.5/24) * agent_hours.start_time * 2) + (rownum * .5/24),'HH24:mi') endtime,
to_timestamp(to_char(((trunc(starts_at) + (.5/24) * agent_hours.start_time * 2) + ((rownum -1) * .5/24)),'DD-MON-YYYY HH24:MI:SS'),'DD-MON-YYYY HH24:MI:SS') full_starttime,
to_timestamp(to_char(((trunc(starts_at) + (.5/24) * agent_hours.start_time * 2) + (rownum * .5/24)),'DD-MON-YYYY HH24:MI:SS'),'DD-MON-YYYY HH24:MI:SS') full_endtime,
starts_at,
ends_at
from agent_hours,
( select rownum
from all_objects
where rownum <= (select end_time - start_time from agent_hours)*2) ) slots
where slots.agent_id = app.agent_id(+);
Yes, it's possible. You leave a lot of details unspecified. I'm assuming you have an agents table as well (agent_id, start_hour, end_hour) with the hour fields being integers between 0 and 24. (This implies that a given agent's workday does not vary over different days.) I'm also assuming that the user inputs driving the query would be an agent_id and a day for which the schedule should be viewed, the latter represented as a DATE.
I'm not able to test this right now, but I think it will essentially do what you want:
with agent_workday as (
select agent_id,
:date + start_hour/24 agent_start_time,
:date + end_hour/24 agent_end_time
from agents
where agent_id = :agent
), agent_slots as (
select agent_id,
level slotnum,
agent_start_time + (level-1)/48 slot_start,
agent_start_time + level/48 slot_end
from agent_workday
connect by level <= (agent_end_time-agent_start_time)*48
)
select :date, slotnum, agent_slots.agent_id,
TO_CHAR(slot_start, 'HH24:MI'), TO_CHAR(slot_end, 'HH24:MI'),
appointment_id
from agent_slots left join appointments
on appointments.agent_id = agent_slots.agent_id
and agent_slots.block_start >= appointments.starts_at
and agent_slots.block_end <= appointments.ends_at
order by slotnum
Try this(not tested):
WITH time_slots AS
(
SELECT b.starts ,
b.starts + (rownum -1) * 0.5/24 starts_at,
b.starts + (rownum) * 0.5/24 ends_at,
rownum AS slotnum
FROM ALL_OBJECTS a,
(
SELECT TRUNC(sysdate) + 9/24 AS starts --Can have the actual date you are looking for instead of SYSDATE
FROM DUAL
) b
)
SELECT a.starts AS "DATE",
a.slotnum,
b.agent_id,
TO_CHAR(a.starts_at, 'HH24:MI') AS starts_at,
TO_CHAR(a.ends_at, 'HH24:MI') AS ends_at,
b.id AS appointment_id
FROM time_slots a LEFT JOIN appointments b
ON (b.starts_at >= a.starts_at AND b.starts_at <= a.ends_at) OR
(b.ends_at >= a.starts_at AND b.starts_at <= a.ends_at)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.