在Oracle中生成两个日期之间的时间间隔的行

问题描述 投票:0回答:1

我有桌子,周日到周六“医生开始”和“结束时间”。我想创建15分钟的时间段。

在此基础上,患者点击日历日期时间间隔,该间隔显示已经预订的插槽。

enter image description here

sql oracle oracle-apex
1个回答
2
投票

以下示例显示如何将时间分成15分钟的切片。它使用分层查询。一点点解释:

  • 第2行:trunc函数,应用于日期值,返回当天的“开始”(午夜)。添加15 / (24*60)需要15分钟(因为一天24小时,一小时60分钟)。乘以level的15作为“循环”,即将15乘15乘15分钟加到之前的值。
  • 第4行:类似于第2行,但它确保一天(24小时* 60分钟)被分成15分钟的部分
  • 第6行:开始时间很简单
  • 第7行:结束时间只增加15分钟到start_time
  • 第9行:只返回10到16小时之间的时间(你没有患者在凌晨02:15,对吧?)

SQL> with fifteen as
  2    (select trunc(sysdate) + (level * 15)/(24*60) c_time
  3     from dual
  4     connect by level <= (24*60) / 15
  5    )
  6  select to_char(c_time, 'hh24:mi') start_time,
  7         to_char(c_time + 15 / (24 * 60), 'hh24:mi') end_time
  8  from fifteen
  9  where extract(hour from cast (c_time as timestamp)) between 10 and 15;

START_TIME END_TIME
---------- ----------
10:00      10:15
10:15      10:30
10:30      10:45
10:45      11:00
11:00      11:15
11:15      11:30
11:30      11:45
11:45      12:00
12:00      12:15
12:15      12:30
12:30      12:45
12:45      13:00
13:00      13:15
13:15      13:30
13:30      13:45
13:45      14:00
14:00      14:15
14:15      14:30
14:30      14:45
14:45      15:00
15:00      15:15
15:15      15:30
15:30      15:45
15:45      16:00

24 rows selected.

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.