Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 422BA632AFC for ; Thu, 2 Apr 2009 10:56:14 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 92007-05 for ; Thu, 2 Apr 2009 10:56:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from lists.commandprompt.com (host-159.commandprompt.net [207.173.203.159]) by mail.postgresql.org (Postfix) with ESMTP id F0E846321B4 for ; Thu, 2 Apr 2009 10:56:09 -0300 (ADT) Received: from alvh.no-ip.org ([186.9.76.36]) (authenticated bits=0) by lists.commandprompt.com (8.13.8/8.13.8) with ESMTP id n32E2sFf005174 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 2 Apr 2009 07:03:04 -0700 Received: by alvh.no-ip.org (Postfix, from userid 1000) id 534BA43825D; Thu, 2 Apr 2009 09:54:04 -0400 (CLT) Date: Thu, 2 Apr 2009 09:54:04 -0400 From: Alvaro Herrera To: James Kitambara Cc: Srikanth , pgsql-sql@postgresql.org Subject: Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps Message-ID: <20090402135404.GD3163@alvh.no-ip.org> References: <19947.64230.qm@web94606.mail.in2.yahoo.com> <49BFBC61.9020006@archonet.com> <849335.93192.qm@web27903.mail.ukl.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <849335.93192.qm@web27903.mail.ukl.yahoo.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0 (lists.commandprompt.com [207.173.203.159]); Thu, 02 Apr 2009 07:03:08 -0700 (PDT) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.185 tagged_above=0 required=5 tests=AWL=0.185 X-Spam-Level: X-Archive-Number: 200904/10 X-Sequence-Number: 32356 James Kitambara wrote: > Dear Srikanth, > You can solve your problem by doing this > > THE SQL IS AS FOLLOWS >   ASSUME TIME INTERVAL 2008-12-07 07:59:59 TO 2008-12-07 08:58:59 AND THE TABLE NAME time_interval > >  COUNT (*) FROM   >     (select customer_id, log_session_id, start_ts, end_ts , end_ts-start_ts as "Interval" from time_interval >      where end_ts-start_ts >= '1 hour' >      and '2008-12-07 07:59:59' between start_ts and end_ts) > AS COUNT ; Another way to phrase the WHERE clause is with the OVERLAPS operator, something like this: WHERE (start_ts, end_ts) OVERLAPS ('2008-12-07 07:59:59', '2008-12-07 08:59:59') What I'm not so sure about is how optimizable this construct is. -- Alvaro Herrera http://www.CommandPrompt.com/ The PostgreSQL Company - Command Prompt, Inc.