Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id B31119FA435 for ; Fri, 26 Jan 2007 06:05:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 09593-09 for ; Fri, 26 Jan 2007 06:05:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from matrix.gatewaynet.com (host2.dynacom.ondsl.gr [62.103.35.210]) by postgresql.org (Postfix) with ESMTP id AF0DB9FA306 for ; Fri, 26 Jan 2007 06:05:03 -0400 (AST) Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=ip216.internal.net) by matrix.gatewaynet.com with esmtp (Exim 4.62) (envelope-from ) id 1HANx7-0006JA-GN for pgsql-sql@postgresql.org; Fri, 26 Jan 2007 12:05:01 +0200 From: Achilleas Mantzios Organization: Dynacom Tankers Mgmt To: pgsql-sql@postgresql.org Subject: Re: Intersection of two date interval Date: Fri, 26 Jan 2007 12:02:24 +0200 User-Agent: KMail/1.9.3 References: <49a67b230701260139n1153ee82x8ae91875118f3cd2@mail.gmail.com> <20070126095007.GD15591@a-kretschmer.de> In-Reply-To: <20070126095007.GD15591@a-kretschmer.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200701261202.26168.achill@matrix.gatewaynet.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/132 X-Sequence-Number: 27284 =CE=A3=CF=84=CE=B9=CF=82 =CE=A0=CE=B1=CF=81=CE=B1=CF=83=CE=BA=CE=B5=CF=85= =CE=AE 26 =CE=99=CE=B1=CE=BD=CE=BF=CF=85=CE=AC=CF=81=CE=B9=CE=BF=CF=82 2007= 11:50, =CE=BF/=CE=B7 A. Kretschmer =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > am Fri, dem 26.01.2007, um 11:39:03 +0200 mailte Suha Onay folgendes: > > Hi, > > > > How can i find the number of days in the intersection of 2 date interva= l? > > For example: > > 1st interval: (10.01.2007, 20.01.2007) > > 2nd interval: (13.01.2007, 21.01.2007) > > The intersection dates are: 13,14,15,16,17,18,19, 20 > > The result is: 8 > > > > How can i find the result, 8 in an sql query without using CASE > > statements? > > Some time ago i wrote a function for this, i hope it's helpfull for you: > (not realy tested, be careful!) > > > > create type start_end as (t1 timestamptz, t2 timestamptz); > > create or replace function time_intersect (timestamptz, timestamptz, > timestamptz, timestamptz) returns start_end as $$ declare > _s1 alias for $1; > _e1 alias for $2; > _s2 alias for $3; > _e2 alias for $4; > _start timestamptz; > _end timestamptz; > _return start_end; > begin > > if _s1 < _s2 then > _start :=3D _s2; > else > _start :=3D _s1; > end if; > > if _e1 < _e2 then > _end :=3D _e1; > else > _end :=3D _e2; > end if; > > if _start < _end then > _return.t1 :=3D _start; > _return.t2 :=3D _end; > else > _return.t1 :=3D NULL; > _return.t2 :=3D NULL; > end if; > > return _return; > end > $$ language plpgsql; > > Suha, the function is the number of days in the=20 maximum of the two start dates , untill , minimum of the two end dates=20 interval. But in postgresql (7.4.15 at least) there is no MIN(date,date),MAX(date,dat= e)=20 functions. So someone has to write them, so you cant avoid some logic there. Whats your problem with "CASE" statements? what you are basically looking for is smth like the above implementation from Andreas. > test=3D# set datestyle=3Dgerman; > SET > test=3D*# select time_intersect('10.01.2007'::date, '20.01.2007'::date, > '13.01.2007'::date, '21.01.2007'::date); time_intersect > ------------------------------------------------------- > ("13.01.2007 00:00:00 CET","20.01.2007 00:00:00 CET") > (1 row) > > > test=3D*# select *, t2-t1 from time_intersect('10.01.2007'::date, > '20.01.2007'::date, '13.01.2007'::date, '21.01.2007'::date); t1 = =20 > | t2 | ?column? > -------------------------+-------------------------+---------- > 13.01.2007 00:00:00 CET | 20.01.2007 00:00:00 CET | @ 7 days > (1 row) > > > > > Andreas =2D-=20 Achilleas Mantzios