Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PostgreSql的预订表中查找第一个空闲时间_Sql_Postgresql_Schedule_Postgresql 9.2 - Fatal编程技术网

如何在PostgreSql的预订表中查找第一个空闲时间

如何在PostgreSql的预订表中查找第一个空闲时间,sql,postgresql,schedule,postgresql-9.2,Sql,Postgresql,Schedule,Postgresql 9.2,预订表包含预订开始日期、开始时间和持续时间。 开始时间按工作时间8:00的半小时递增。。工作日18:00。 持续时间也以天为单位以半小时为增量 CREATE TABLE reservation ( startdate date not null, -- start date starthour numeric(4,1) not null , -- start hour 8 8.5 9 9.5 .. 16.5 17 17.5 duration Numeric(3,1) not

预订表包含预订开始日期、开始时间和持续时间。 开始时间按工作时间8:00的半小时递增。。工作日18:00。 持续时间也以天为单位以半小时为增量

CREATE TABLE reservation (
  startdate date not null,  -- start date
  starthour numeric(4,1) not null , -- start hour 8 8.5 9 9.5  ..  16.5 17 17.5
  duration  Numeric(3,1) not null, -- duration by hours 0.5 1 1.5 .. 9 9.5 10
  primary key (startdate, starthour)
);
如果需要,可以更改表结构

如何在未预订的桌子上找到前半小时的空闲时间? 如果表中包含

startdate   starthour  duration 
14          9           1              -- ends at 9:59
14          10          1.5            -- ends at 11:29, e.q there is 30 minute gap before next
14          12          2
14          16          2.5
结果应该是:

starthour  duration
11.5       0.5
可能应该使用PostgreSql 9.2窗口函数来查找 第一行的开始时间大于前一行的开始时间+持续时间
如何编写返回此信息的select语句?

可能不是最好的查询,但它可以满足您的需要:

具有 倍 选择开始日期, 开始日期+楼层开始时间| | h'|| starthour floorstarthour*60 | | | min::间隔时间, 开始日期+楼层开始时间| | h'|| starthour floorstarthour*60 | | min::间隔 +楼龄| |“h”|| 持续时间下限持续时间*60 | |分钟::间隔小时 从预定开始, 差距作为 选择sdate、shour、ehour、leadshour、1、ehour 按shour-ehour间隙按sdate顺序进行的过分割 从时代 从间隙>'0'的间隙中选择*::间隔; 一些注意事项:

最好不要将事件的时间和数据分开。如果必须,则使用标准类型; 如果无法使用标准类型,请创建函数将数字小时转换为时间格式。
Postgres 9.2具有范围类型,我建议使用它们

创建表保留tsrange; 插入到保留值中 '[2012-11-14 09:00:00,2012-11-14 10:00:00', '[2012-11-14 10:00:00,2012-11-14 11:30:00', '[2012-11-14 12:00:00,2012-11-14 14:00:00', '[2012-11-14 16:00:00,2012-11-14 18:30:00'; ALTER TABLE RESERATION ADD EXCLUDE使用gist RESERATION WITH&&; 使用gist排除将创建不允许插入重叠项的索引。您可以使用以下查询查找vyegorov查询的变体:

with gaps as (
  select 
    upper(reservation) as start, 
    lead(lower(reservation),1,upper(reservation)) over (ORDER BY reservation) - upper(reservation) as gap 
  from (
    select * 
    from reservation 
    union all values 
      ('[2012-11-14 00:00:00, 2012-11-14 08:00:00)'::tsrange), 
      ('[2012-11-14 18:00:00, 2012-11-15 00:00:00)'::tsrange)
  ) as x
) 
select * from gaps where gap > '0'::interval;
“联合所有值”屏蔽了非工作时间,因此您只能在上午8点到下午18点之间预订

结果如下:

        start        |   gap    
---------------------+----------
 2012-11-14 08:00:00 | 01:00:00
 2012-11-14 11:30:00 | 00:30:00
 2012-11-14 14:00:00 | 02:00:00
文档链接: -范围类型
-

同意-如果必须将时间与日期分开存储,请使用时间。不要使用数字;从上面的查询中可以看出,这会使时间和间隔的处理变得复杂。此处交叉发布到PostgreSQL邮件列表:。不向下投票,因为作者没有从Pg列表中链接到此问题。这与您非常相似。这发现仅在一天内存在差距。如何在工作时间8:00..18:00的假日表中查找除周六、周日和公共假日以外的任何工作日的差距?我在中以单独问题的形式发布了此问题,我正在尝试为库存订单创建相同的“差距”,也许您可以看看?