【发布时间】:2012-11-14 20:57:48
【问题描述】:
预订表包含预订开始日期、开始时间和持续时间。 开始时间按工作日 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)
);
如果需要,可以更改表结构。
如何在未保留的表中找到第一个空闲半小时? E.q 如果表包含
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 的窗口函数应该用来查找
starthour 大于前一行 starthour + 持续时间的第一行
如何编写返回此信息的 select 语句?
【问题讨论】:
-
在此处交叉发布到 PostgreSQL 邮件列表:archives.postgresql.org/message-id/…。不反对,因为作者确实从 Pg 列表帖子链接到这个问题。
标签: sql postgresql schedule postgresql-9.2