Re: date ranges in where

From: Miguel Miranda <miguel(dot)mirandag(at)gmail(dot)com>
To: Erik Jones <ejones(at)engineyard(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: date ranges in where
Date: 2009-05-06 21:48:31
Message-ID: aa6b08350905061448m2124609al9f79552fa2b24edc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Well, i tried all your sugestions, and i found some funny issues, i use the
query to count exactly in a day by day basis, and running the query with

WHERE lastlogin >= '2009-05-01' AND lastlogin < '2009-05-03'

OR

WHERE lastlogin >= 'X' AND lastlogin <= 'Y' + '1 day'::interval

it includes the 0 hours of day 3:

05-02-2009 12:00:00 AM

The exact result are from running:

....between '2009-05-01 00:00'::timestamp
and '2009-05-02 23:59:59'::timestamp

but i select the ranges from a web form using a textbox, and right now i
dont have a java calendar at hand, i tried this with good result:

WHERE lastlogin::date BETWEEN '2009-05-01' AND '2009-05-02'

But now the query uses seq scan and not the index in lastlogin column.

Is there another way?

On Wed, May 6, 2009 at 3:17 PM, Erik Jones <ejones(at)engineyard(dot)com> wrote:

>
> On May 6, 2009, at 2:12 PM, Miguel Miranda wrote:
>
> Hi, what is the recommended way to select a range of dates?
>>
>> Lets say a have a table with a lastlogin (timestamp) column and i want
>> toknow what users logged in for last time between 2009-05-01 and 2009-05-02?
>>
>> I know that a simple
>>
>> where lastlogin between '2009-05-01' and '2009-05-02' doesnt work beacuse
>> it doesnt include who logged in 2009-05-02 15:30:00, etc...
>>
>
>
> WHERE lastlogin >= '2009-05-01' AND lastlogin < '2009-05-03'
>
> or, if the values have are some unknown X and Y dates then you can do this:
>
> WHERE lastlogin >= 'X' AND lastlogin <= 'Y' + '1 day'::interval
>
> Erik Jones, Database Administrator
> Engine Yard
> Support, Scalability, Reliability
> 866.518.9273 x 260
> Location: US/Pacific
> IRC: mage2k
>
>
>
>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Erik Jones 2009-05-06 21:51:08 Re: date ranges in where
Previous Message John R Pierce 2009-05-06 21:45:25 Re: XML -> PG ?