预订表包含预订的开始日期、开始时间和持续时间。开始时间以工作日的半小时为增量,在工作时间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的窗口函数来查找第一行,其starthour大于上一行starthour + duration
如何编写选择语句以返回此信息?