Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 393B46335AF for ; Fri, 10 Apr 2009 19:47:01 -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 37282-03 for ; Fri, 10 Apr 2009 19:46:53 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from oberon.wxnz.net (oberon.wxnz.net [58.28.6.13]) by mail.postgresql.org (Postfix) with ESMTP id 591CF633593 for ; Fri, 10 Apr 2009 19:46:59 -0300 (ADT) Received: from gonzo (ip-118-90-140-51.xdsl.xnet.co.nz [118.90.140.51]) by oberon.wxnz.net (Postfix) with ESMTP id B052B46C442 for ; Sat, 11 Apr 2009 10:46:55 +1200 (NZST) Received: from news by gonzo with local (Exim 4.69) (envelope-from ) id 1LsPQ2-00042J-GF for pgsql-sql@postgresql.org; Sat, 11 Apr 2009 10:41:54 +1200 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps Date: 10 Apr 2009 22:41:54 GMT Organization: Dis (not Dat) Organisation Lines: 38 Message-ID: References: <19947.64230.qm@web94606.mail.in2.yahoo.com> <49BFBC61.9020006@archonet.com> <849335.93192.qm@web27903.mail.ukl.yahoo.com> <20090402135404.GD3163@alvh.no-ip.org> NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1239403314 15517 127.0.0.1 (10 Apr 2009 22:41:54 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-2 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200904/53 X-Sequence-Number: 32399 On 2009-04-02, Alvaro Herrera wrote: > 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. > http://www.postgresql.org/docs/8.3/interactive/xindex.html if you gave the apropriate GIST index on (start_ts, end_ts) the overlaps may be optimisable. the subquery will run to completion and count will count the results. - but this form gives different results. beter to do select COUNT (*) AS COUNT FROM time_interval WHERE (start_ts, end_ts) OVERLAPS ('2008-12-07 07:59:59', '2008-12-07 08:59:59') or select COUNT (*) AS COUNT FROM time_interval where end_ts-start_ts >= '1 hour' and '2008-12-07 07:59:59' between start_ts and end_ts;