Re: Counting days ... - Mailing list pgsql-sql

From Steve Crawford
Subject Re: Counting days ...
Date
Msg-id 47D98D11.4080406@pinpointresearch.com
Whole thread Raw
In response to Re: Counting days ...  (Aarni Ruuhimäki <aarni@kymi.com>)
Responses Re: Counting days ...  (Aarni Ruuhimäki <aarni@kymi.com>)
List 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



pgsql-sql by date:

Previous
From: Aarni Ruuhimäki
Date:
Subject: Re: Counting days ...
Next
From: chester c young
Date:
Subject: cursors and sessions