pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps
Date: 10 Apr 2009 22:41:54 GMT
Message-ID: <grohvi$f4t$1@reversiblemaps.ath.cx> (raw)
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>

On 2009-04-02, Alvaro Herrera <alvherre@commandprompt.com> 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;
 



view thread (5+ messages)  latest in thread

Message-ID: <grohvi$f4t$1@reversiblemaps.ath.cx>
Permalink:  ../grohvi$f4t$1@reversiblemaps.ath.cx/
Also on:    postgresql.org/message-id/grohvi$f4t$1@reversiblemaps.ath.cx

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasen@xnet.co.nz
  Subject: Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps
  In-Reply-To: <grohvi$f4t$1@reversiblemaps.ath.cx>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox