Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id CDA479FA4DD for ; Tue, 12 Dec 2006 18:23:19 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 77891-06 for ; Tue, 12 Dec 2006 18:23:12 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from lists.commandprompt.com (host-130.commandprompt.net [207.173.203.130]) by postgresql.org (Postfix) with ESMTP id 7E9A39FA49E for ; Tue, 12 Dec 2006 18:23:12 -0400 (AST) Received: from perhan.alvh.no-ip.org (200-126-100-202.bk7-dsl.surnet.cl [200.126.100.202]) (authenticated bits=0) by lists.commandprompt.com (8.13.7/8.13.6) with ESMTP id kBCMNCON019463; Tue, 12 Dec 2006 14:23:13 -0800 Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 76384329FA; Tue, 12 Dec 2006 19:23:08 -0300 (CLST) Date: Tue, 12 Dec 2006 19:23:08 -0300 From: Alvaro Herrera To: Marcin =?utf-8?Q?St=C4=99pnicki?= Cc: pgsql-sql@postgresql.org Subject: Re: Finding gaps in scheduled events Message-ID: <20061212222308.GP22782@alvh.no-ip.org> Mail-Followup-To: Marcin =?utf-8?Q?St=C4=99pnicki?= , pgsql-sql@postgresql.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: ClamAV version 0.88.5, clamav-milter version 0.88.5 on projects.commandprompt.com X-Virus-Status: Clean X-Greylist: Sender succeded SMTP AUTH authentication, not delayed by milter-greylist-1.6 (lists.commandprompt.com [192.168.2.159]); Tue, 12 Dec 2006 14:23:13 -0800 (PST) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/96 X-Sequence-Number: 27052 Marcin Stępnicki wrote: > Now I need to create a query to find hours at which each of the type can > start. So, if it's event A (which take 15 minutes) it can start at: > > 8:00 (to 8:15) > 8:15 (to 8:30) > ( 8:30 to 8:45 is already taken ) > 8:45 (to 9:00) > 9:00 (to 9:15) > 9:15 (to 9:30) > ( 9:30 to 10:00 (9:30-9:45 and 9:45-10:00) is already taken)) > 10:00 (to 10:15) I think something like this should help you: select my_hour from test_events right join test_timeline on ((start, finish) overlaps (my_hour, my_hour + 15 * '1 minute'::interval)) where start is null; With your test data, it shows all the times except for 8:30, 9:30 and 9:45.