Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 03A1C2E01F4 for ; Fri, 14 Mar 2008 10:04:47 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 21433-05 for ; Fri, 14 Mar 2008 10:04:41 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtp20.kymp.net (smtp20.kymp.net [80.248.96.137]) by postgresql.org (Postfix) with ESMTP id F40FC2E01F5 for ; Fri, 14 Mar 2008 10:04:40 -0300 (ADT) Received: from kymifedora ([213.130.241.66]) by smtp20.kymp.net (8.13.3/8.13.3) with ESMTP id m2ED4aCK019649; Fri, 14 Mar 2008 15:04:36 +0200 From: Aarni =?iso-8859-1?q?Ruuhim=E4ki?= Reply-To: aarni@kymi.com Organization: KYMI.com To: pgsql-sql@postgresql.org Subject: Re: Counting days ... Date: Fri, 14 Mar 2008 15:04:54 +0200 User-Agent: KMail/1.6.2 References: <200803132025.27821.aarni@kymi.com> <200803132209.33914.aarni@kymi.com> <47D98D11.4080406@pinpointresearch.com> In-Reply-To: <47D98D11.4080406@pinpointresearch.com> Cc: Steve Crawford MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-Id: <200803141504.54211.aarni@kymi.com> X-Virus-Scanned: by amavisd-new X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/80 X-Sequence-Number: 30408 > > 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 Thanks Steve, I'm not sure if I quite grasped this. It gives a bit funny results: SELECT sum ((date_smaller(res_end_day, '2007-12-31'::date) -=20 date_larger(res_start_day, '2006-12-31'::date)) * group_size) AS=20 days_in_period, c.country_name AS country =46ROM product_res pr LEFT JOIN countries c ON pr.country_id =3D c.country_id WHERE group_id =3D 1 AND res_end_day >=3D '2007-01-01' AND res_end_day <=3D= =20 '2008-12-31' group by pr.country_id, c.country_name; days_in_period | country =2D---------------+-------------------- -441137 | -30 | Germany -28 | Estonia 60 | Bulgaria -25003 | Russian Federation -207670 | Suomi 256 | Ukraine -6566 | Latvia -280 | United States -1889 | Switzerland 114 | Lithuania 36 | Norway -66 | Sweden 170 | Kazakhstan 72 | Belarus (15 rows) Anyway, I have to rethink and elaborate the query. I know that it will usua= lly=20 be on a monthly or yearly basis, but a reservation can actually be any of t= he=20 following in relation to the given (arbitrary) period: 1. start_day before period_start, end_day =3D period_start 2. start_day before period_start, end_day in period 3. start_day before period_start, end_day =3D period_end 4. start_day =3D period_start, end_day in period 5. start_day in period, end_day in period 6. start_day =3D period_start, end_day =3D period_end 7. start_day in period, end_day =3D period_end 8. start_day in period, end_day after period_end 9. start_day =3D period_start, end_day =3D period_end 10 start_day before period_start, end_day after period_end Hmm ... Best regards, =2D-=20 Aarni Ruuhim=E4ki =2D-- Burglars usually come in through your windows. =2D--