Sql 获取日期和时间范围之间的教室可用小时数

Sql 获取日期和时间范围之间的教室可用小时数,sql,oracle,datetime,plsql,Sql,Oracle,Datetime,Plsql,我正在使用Oracle 11g,我遇到了这个问题。我还想不出任何办法来解决它 我有一张有人坐的桌子。我需要找到的是datetime范围内的可用小时数。例如,我有A、B和C房间,占用教室的桌子如下所示: Classroom start end A 10/10/2013 10:00 10/10/2013 11:30 B 10/10/2013 09:15 10/10/2013 10:4

我正在使用Oracle 11g,我遇到了这个问题。我还想不出任何办法来解决它

我有一张有人坐的桌子。我需要找到的是datetime范围内的可用小时数。例如,我有A、B和C房间,占用教室的桌子如下所示:

Classroom        start                 end  
   A         10/10/2013 10:00      10/10/2013 11:30  
   B         10/10/2013 09:15      10/10/2013 10:45  
   B         10/10/2013 14:30      10/10/2013 16:00  
我需要得到的是这样的东西:

Classroom        start                 end  
   A         10/10/2013 10:00      10/10/2013 11:30  
   B         10/10/2013 09:15      10/10/2013 10:45  
   B         10/10/2013 14:30      10/10/2013 16:00  
日期时间范围在“2013年10月10日07:00”和“2013年10月10日21:15”之间


有没有一种方法可以通过sql或pl/sql来实现这一点?

当您想要选择一些不存在的东西时,这总是一个挑战。首先,你需要一份15分钟内所有可用教室和时间的列表。然后,您可以通过跳过已占用的项目来选择它们

我设法在没有任何PL/SQL的情况下进行了查询:

CREATE TABLE Table1
    (Classroom VARCHAR2(10), start_ts DATE, end_ts DATE);    
INSERT INTO Table1 VALUES ('A', TIMESTAMP '2013-01-10 10:00:00', TIMESTAMP '2013-01-10 11:30:00');
INSERT INTO Table1 VALUES ('B', TIMESTAMP '2013-01-10 09:15:00', TIMESTAMP '2013-01-10 10:45:00');
INSERT INTO Table1 VALUES ('B', TIMESTAMP '2013-01-10 14:30:00', TIMESTAMP '2013-01-10 16:00:00');



WITH all_rooms AS
    (SELECT CHR(64+LEVEL) AS ROOM FROM dual CONNECT BY LEVEL <= 3),
all_times AS
    (SELECT  CAST(TIMESTAMP '2013-01-10 07:00:00' + (LEVEL-1) * INTERVAL '15' MINUTE AS DATE) AS TIMES, LEVEL AS SLOT
    FROM DUAL
    CONNECT BY TIMESTAMP '2013-01-10 07:00:00' + (LEVEL-1) * INTERVAL '15' MINUTE <= TIMESTAMP '2013-01-10 21:15:00'),
all_free_slots AS
    (SELECT ROOM, TIMES, SLOT, 
        CASE SLOT-LAG(SLOT, 1, 0) OVER (PARTITION BY ROOM ORDER BY SLOT) 
            WHEN 1 THEN 0
            ELSE 1
        END AS NEW_WINDOW 
    FROM all_times
        CROSS JOIN all_rooms
    WHERE NOT EXISTS 
        (SELECT 1 FROM TABLE1 WHERE ROOM = CLASSROOM AND TIMES BETWEEN START_TS + INTERVAL '1' MINUTE AND END_TS - INTERVAL '1' MINUTE)),
free_time_windows AS
    (SELECT ROOM, TIMES, SLOT, 
        SUM(NEW_WINDOW) OVER (PARTITION BY ROOM ORDER BY SLOT) AS WINDOW_ID
    FROM all_free_slots)
SELECT ROOM, 
    TO_CHAR(MIN(TIMES), 'yyyy-mm-dd hh24:mi') AS free_time_start, 
    TO_CHAR(MAX(TIMES), 'yyyy-mm-dd hh24:mi') AS free_time_end
FROM free_time_windows
GROUP BY ROOM, WINDOW_ID
HAVING MAX(TIMES) - MIN(TIMES) > 0
ORDER BY ROOM, 2;


ROOM FREE_TIME_START    FREE_TIME_END
---- ----------------------------------
A    2013-01-10 07:00   2013-01-10 10:00
A    2013-01-10 11:30   2013-01-10 21:15
B    2013-01-10 07:00   2013-01-10 09:15
B    2013-01-10 10:45   2013-01-10 14:30
B    2013-01-10 16:00   2013-01-10 21:15
C    2013-01-10 07:00   2013-01-10 21:15
为了理解查询,您可以从顶部拆分子查询,例如

WITH all_rooms AS
    (SELECT CHR(64+LEVEL) AS ROOM FROM dual CONNECT BY LEVEL <= 3),
all_times AS
    (SELECT  CAST(TIMESTAMP '2013-01-10 07:00:00' + (LEVEL-1) * INTERVAL '15' MINUTE AS DATE) AS TIMES, LEVEL AS SLOT
    FROM DUAL
    CONNECT BY TIMESTAMP '2013-01-10 07:00:00' + (LEVEL-1) * INTERVAL '15' MINUTE <= TIMESTAMP '2013-01-10 21:15:00')
SELECT ROOM, TIMES, SLOT, 
    CASE SLOT-LAG(SLOT, 1, 0) OVER (PARTITION BY ROOM ORDER BY SLOT) 
        WHEN 1 THEN 0
        ELSE 1
    END AS NEW_WINDOW 
FROM all_times
    CROSS JOIN all_rooms
WHERE NOT EXISTS (SELECT 1 FROM TABLE1 WHERE ROOM = CLASSROOM AND TIMES BETWEEN START_TS + INTERVAL '1' MINUTE AND END_TS - INTERVAL '1' MINUTE)
ORDER BY ROOM, SLOT

我正在寻找一个在概念上至少与Wernfried的类似的解决方案,但我认为它也足够不同。开始时也是一样,首先生成可能的时间段,并假设您看到的是15分钟的窗口:我使用CTE,因为我认为它们比嵌套选择更清晰,尤其是在这么多级别的情况下

with date_time_range as (
  select to_date('10/10/2013 07:00', 'DD/MM/YYYY HH24:MI') as date_start,
    to_date('10/10/2013 21:15', 'DD/MM/YYYY HH24:MI') as date_end
  from dual
),
time_slots as (
  select level as slot_num,
    dtr.date_start + (level - 1) * interval '15' minute as slot_start,
    dtr.date_start + level * interval '15' minute as slot_end
  from date_time_range dtr
  connect by level <= (dtr.date_end - dtr.date_start) * (24 * 4) -- 15-minutes
)
select * from time_slots;
但是你必须把它们折叠成连续的范围。有多种方法可以做到这一点;在这里,我查看上一行和下一行,以确定特定值是否为范围的边缘:

with date_time_range as (...),
time_slots as (...),
free_slots as (...),
free_slots_extended as (
  select fs.classroom, fs.slot_num,
    case when fs.lag_end is null or fs.lag_end != fs.slot_start
      then fs.slot_start end as slot_start,
    case when fs.lead_start is null or fs.lead_start != fs.slot_end
      then fs.slot_end end as slot_end
  from free_slots fs
)
select * from free_slots_extended
where (fse.slot_start is not null or fse.slot_end is not null);
现在我们只剩下12排了。外部where子句消除了上一步中153个插槽中的所有141个,这些插槽是中等范围的,因为我们只关心边缘:

CLASSROOM   SLOT_NUM SLOT_START       SLOT_END       
--------- ---------- ---------------- ----------------
A                  1 2013-10-10 07:00                  
A                 12                  2013-10-10 10:00 
A                 19 2013-10-10 11:30                  
A                 57                  2013-10-10 21:15 
B                  1 2013-10-10 07:00                  
B                  9                  2013-10-10 09:15 
B                 16 2013-10-10 10:45                  
B                 30                  2013-10-10 14:30 
B                 37 2013-10-10 16:00                  
B                 57                  2013-10-10 21:15 
C                  1 2013-10-10 07:00                  
C                 57                  2013-10-10 21:15 
因此,这些表示边,但在单独的行上,最后一步将它们组合在一起:

...
select distinct fse.classroom,
  nvl(fse.slot_start, lag(fse.slot_start)
    over (partition by fse.classroom order by fse.slot_num)) as slot_start,
  nvl(fse.slot_end, lead(fse.slot_end)
    over (partition by fse.classroom order by fse.slot_num)) as slot_end
from free_slots_extended fse
where (fse.slot_start is not null or fse.slot_end is not null)
或者把所有这些放在一起:

with date_time_range as (
  select to_date('10/10/2013 07:00', 'DD/MM/YYYY HH24:MI') as date_start,
    to_date('10/10/2013 21:15', 'DD/MM/YYYY HH24:MI') as date_end
  from dual
),
time_slots as (
  select level as slot_num,
    dtr.date_start + (level - 1) * interval '15' minute as slot_start,
    dtr.date_start + level * interval '15' minute as slot_end
  from date_time_range dtr
  connect by level <= (dtr.date_end - dtr.date_start) * (24 * 4) -- 15-minutes
),
free_slots as (
  select c.classroom, ts.slot_num, ts.slot_start, ts.slot_end,
    lag(ts.slot_end) over (partition by c.classroom order by ts.slot_num)
      as lag_end,
    lead(ts.slot_start) over (partition by c.classroom order by ts.slot_num)
      as lead_start
  from time_slots ts
  cross join classrooms c
  left join occupied_classrooms oc on oc.classroom = c.classroom
    and not (oc.occupied_end <= ts.slot_start
      or oc.occupied_start >= ts.slot_end)
  where oc.classroom is null
),
free_slots_extended as (
  select fs.classroom, fs.slot_num,
    case when fs.lag_end is null or fs.lag_end != fs.slot_start
      then fs.slot_start end as slot_start,
    case when fs.lead_start is null or fs.lead_start != fs.slot_end
      then fs.slot_end end as slot_end
  from free_slots fs
)
select distinct fse.classroom,
  nvl(fse.slot_start, lag(fse.slot_start)
    over (partition by fse.classroom order by fse.slot_num)) as slot_start,
  nvl(fse.slot_end, lead(fse.slot_end)
    over (partition by fse.classroom order by fse.slot_num)) as slot_end
from free_slots_extended fse
where (fse.slot_start is not null or fse.slot_end is not null)
order by 1, 2;

.

你现在对正常运行有什么想法?我现在所拥有的是这样的:选择ui.*从已占用的oc类别中,选择日期'10/10/2013 07:00','dd/MM/RR hh24:mi'在oc.start和oc.end之间,或选择日期'10/10/2013 21:15',“dd/MM/RR hh24:mi”介于oc.start和oc.end之间,或oc.start介于日期“2013年10月10日07:00”、“dd/MM/RR hh24:mi”和日期“2013年10月10日21:15”、“dd/MM/RR hh24:mi”或oc.end介于日期“2013年10月10日07:00”、“dd/MM/RR hh24:mi”和日期“2013年10月10日21:15”、“dd/MM/RR hh24:mi”之间。但这只会让我得到C教室。我只会让我得到C教室。你可以用PL\SQL来做。很好,如果你知道你的片段,比如说,15分钟的片段。然后可以为每个房间创建15分钟分段的阵列。你会知道你有多少部分,因为有房间时间。比如说早上7点到下午6点——你有44个片段。然后根据开始和结束时间填充这些分段。然后,读取空段并创建可用的时间跨度。例如,如果第3段、第4段和第5段是emply,您可以说上午7:30到8:15有房间。是的,您是对的,我错过了。也许你需要在分区上玩一点CASE SLOT-LAGSLOT,1,0按房间顺序按插槽,当1然后0其他1作为新窗口结束时,或者开始和结束之间的时间可能在某个地方增加或减少一分钟。是的;遗憾的是,我不能再升级了,真的*8-找到窗口比我的尝试要简单得多。很好。@Wernfried它工作正常,但正如我在“是”中所显示的,它在相邻的几个小时内有问题,我明白了。我在条款中加了这一行,然后就行了。拥有MAXTIMES-MINTIMES>0,我对这些事情不了解一半。这是我在stackoverflow的第一个问题,我很高兴这里有很多人愿意帮忙。我希望我能很快帮助这里的其他人,但我必须学会很多,不是吗!看到你最近的评论,我很高兴。
with date_time_range as (
  select to_date('10/10/2013 07:00', 'DD/MM/YYYY HH24:MI') as date_start,
    to_date('10/10/2013 21:15', 'DD/MM/YYYY HH24:MI') as date_end
  from dual
),
time_slots as (
  select level as slot_num,
    dtr.date_start + (level - 1) * interval '15' minute as slot_start,
    dtr.date_start + level * interval '15' minute as slot_end
  from date_time_range dtr
  connect by level <= (dtr.date_end - dtr.date_start) * (24 * 4) -- 15-minutes
),
free_slots as (
  select c.classroom, ts.slot_num, ts.slot_start, ts.slot_end,
    lag(ts.slot_end) over (partition by c.classroom order by ts.slot_num)
      as lag_end,
    lead(ts.slot_start) over (partition by c.classroom order by ts.slot_num)
      as lead_start
  from time_slots ts
  cross join classrooms c
  left join occupied_classrooms oc on oc.classroom = c.classroom
    and not (oc.occupied_end <= ts.slot_start
      or oc.occupied_start >= ts.slot_end)
  where oc.classroom is null
),
free_slots_extended as (
  select fs.classroom, fs.slot_num,
    case when fs.lag_end is null or fs.lag_end != fs.slot_start
      then fs.slot_start end as slot_start,
    case when fs.lead_start is null or fs.lead_start != fs.slot_end
      then fs.slot_end end as slot_end
  from free_slots fs
)
select distinct fse.classroom,
  nvl(fse.slot_start, lag(fse.slot_start)
    over (partition by fse.classroom order by fse.slot_num)) as slot_start,
  nvl(fse.slot_end, lead(fse.slot_end)
    over (partition by fse.classroom order by fse.slot_num)) as slot_end
from free_slots_extended fse
where (fse.slot_start is not null or fse.slot_end is not null)
order by 1, 2;
CLASSROOM SLOT_START       SLOT_END       
--------- ---------------- ----------------
A         2013-10-10 07:00 2013-10-10 10:00 
A         2013-10-10 11:30 2013-10-10 21:15 
B         2013-10-10 07:00 2013-10-10 09:15 
B         2013-10-10 10:45 2013-10-10 14:30 
B         2013-10-10 16:00 2013-10-10 21:15 
C         2013-10-10 07:00 2013-10-10 21:15