简体   繁体   English

帮助 Oracle SQL 预约安排

[英]Help with Oracle SQL for appointment scheduling

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)并且给定一个代理时间表从上午 9:00 开始,到下午 6:00 (18:00) 结束

and given an appointment scheduled between 10:00AM and 11:00AM on 2011-01-01并在 2011 年 1 月 1 日上午 10:00 到 11:00 之间进行了预约

would it be possible to divide his schedule in multiples of 30 minutes能不能把他的日程安排成 30 分钟的倍数

and get the following result using SQL in Oracle:并使用 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)

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.我假设您也有一个代理表(agent_id、start_hour、end_hour),小时字段是 0 到 24 之间的整数。(这意味着给定代理的工作日在不同的日子里没有变化。)我还假设驱动查询的用户输入将是 agent_id 和应查看日程的日期,后者表示为 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM