Re: Counting days ...

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: aarni(at)kymi(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Counting days ...
Date: 2008-03-13 20:22:41
Message-ID: 47D98D11.4080406@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


> Sorry, my bad, I meant 5 days x 4 persons = 20 person-days. From 10 to 15 it
> is 5 'days' ? Arrival 10th and departure 15th makes 5 'nights' or 'days',
> whatever one might call it, statistical accommodation units.
>
> Likewise, res_id x, start_day some time before Feb 08, end_day 2008-02-16 for
> a statistical period Feb 08 makes 16 units ?
>
>
>> First, to simply establish upper and lower bounds,
>> date_larger/date_smaller seems a lot easier - ie. for February inclusive
>> dates:
>>
>> select
>> sum (
>> ((date_smaller(res_end_day, '2008-02-29'::date)
>> - date_larger(res_start_day, '2008-02-01'::date))::int + 1) * group_size
>> ) as person_days;
>>
>>
Check my work, but I think the sum part of the query simply becomes:

sum (
(
date_smaller(res_end_day, '2008-02-29'::date) -
date_larger(res_start_day, '2008-01-31'::date)
) * group_size
)

Basically remove the "+1" so we don't include both start and end dates
but move the start base back one day so anyone starting prior to Feb 1
gets the extra day added.

Cheers,
Steve

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message chester c young 2008-03-13 22:54:33 cursors and sessions
Previous Message Aarni Ruuhimäki 2008-03-13 20:09:33 Re: Counting days ...