Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TLnhN-0000OU-9k for pgsql-sql@postgresql.org; Wed, 10 Oct 2012 04:15:09 +0000 Received: from plane.gmane.org ([80.91.229.3]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TLnhG-0003g9-4l for pgsql-sql@postgresql.org; Wed, 10 Oct 2012 04:15:07 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1TLnhI-0000y6-3P for pgsql-sql@postgresql.org; Wed, 10 Oct 2012 06:15:04 +0200 Received: from ip-118-90-102-36.xdsl.xnet.co.nz ([118.90.102.36]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 10 Oct 2012 06:15:04 +0200 Received: from jasen by ip-118-90-102-36.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 10 Oct 2012 06:15:04 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: checking the gaps in intervals Date: 7 Oct 2012 01:30:37 GMT Organization: Dis (not Dat) Organisation Lines: 54 Message-ID: References: <-3205649711969780110@unknownmsgid> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-102-36.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -4.0 (----) X-Archive-Number: 201210/35 X-Sequence-Number: 36906 On 2012-10-05, Anton Gavazuk wrote: > Hi dear community, > > Have probably quite simple task but cannot find the solution, > > Imagine the table A with 2 columns start and end, data type is date > > start end > 01 dec. 10 dec > 11 dec. 13 dec > 17 dec. 19 dec > ..... > > If I have interval, for example, 12 dec-18 dec, how can I determine > that the interval cannot be fully covered by values from table A > because of the gap 14-16 dec? Looking for solution and unfortunately > nothing has come to the mind yet... perhaps you can do a with-recursive query ? create temp table Gavazuk (id serial primary key, start date ,fin date); insert into Gavazuk (start,fin) values ('2012-12-01','2012-12-10') ,('2012-12-11','2012-12-13') ,('2012-12-17','2012-12-19'); -- this version treats ('2012-12-01','2012-12-10') ('2012-12-11','2012-12-13') -- as contiguous with recursive a as ( select max (fin) as f from Gavazuk where ('2012-12-12') between start and fin union all select distinct (fin) from gavazuk,a where a.f+1 between start and fin and start <= '2012-12-12' ) select max(f) >= '2012-12-18' from a; -- this version treats ('2012-12-01','2012-12-10') ('2012-12-11','2012-12-13') -- as non-contiguous with recursive a as ( select max (fin) as f from Gavazuk where ('2012-12-12') between start and fin union all select distinct (fin) from gavazuk,a where a.f between start and fin-1 and start <= '2012-12-12' ) select max(f) >= '2012-12-18' from a; -- ⚂⚃ 100% natural