帮助Oracle SQL进行预约安排

时间:2011-07-22 00:25:08

标签: sql oracle

我有一个约会表,其中包含以下信息:

id
agent_id
starts_at
ends_at
...

并且代理人时间表从上午9:00开始,到下午6:00(18:00)结束

并在2011-01-01上午10:00到11:00之间安排预约

是否可以将他的日程安排在30分钟的倍数

并使用Oracle中的SQL获得以下结果:

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)

提前致谢。

3 个答案:

答案 0 :(得分:2)

我在做这项工作时自己学到了很多东西。希望这能让您了解一种方法。我的示例在每个表中都有一条记录,因此有了更多记录,您可能需要调整它以获得性能......

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(+);

答案 1 :(得分:0)

是的,这是可能的。你没有详细说明许多细节。我假设你也有一个代理人表(agent_id,start_hour,end_hour),小时字段是0到24之间的整数。(这意味着给定代理人的工作日不会在不同的日子里变化。)我也假设驱动查询的用户输入将是agent_id和应该查看日程表的日期,后者表示为日期。

我现在无法对此进行测试,但我认为它基本上可以满足您的需求:

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

答案 2 :(得分:0)

试试这个(未经测试):

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)