Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps
Date: 2009-03-24 09:39:30
Message-ID: gqa9oi$kv1$4@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 2009-03-17, Srikanth <rssrik(at)yahoo(dot)co(dot)in> wrote:

> Dear all,
>
> I have a table that records User Login Sessions with two timestamp fields. =
> Basically Start of Session and End of a Session (start_ts and end_ts). Each=
> row in the table identifies a session which a customer has used.=A0=20
>
>
> I have to find out how many User Sessions that were present in any given "1=
> HOUR TIME PERIOD".=A0 A single User Session can span across many days.

select count(*) from session
WHERE start_ts < TIME + '1 hour'::interval
AND end_ts >= TIME;

(replace both ocurrences of TIME with the time the interval starts)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2009-03-24 14:35:31 Re: Alter Table/Indexing
Previous Message Jasen Betts 2009-03-24 09:28:58 Re: cast bool/int