Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb7tl-00059h-Kx for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 17:18:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fb7tj-00056A-Uy for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 17:18:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb7tj-000563-NB for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 17:18:27 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb7tc-0007Ky-TQ for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 17:18:27 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id BD52121BD1; Thu, 5 Jul 2018 13:18:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 05 Jul 2018 13:18:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=011gI+BgGTRjW9meQFSGPKOwc0Q8X k0xnexizoH14qo=; b=0tmiObMCxgFYrz/2i6hv76XlDG4xCJgk3YrXVI1CYeU1O mBPA0UfA3wBg1XUN/xhEiL/ZpdpMl6ICMnfd9BRCk7cam2vIfBNHb/uF694oRf1B 04y2D90GyA86i5h5uETTTM/y370cXfQ+e2fF3qb4owewDx+fb+KHBDaCEngvWuih D61g6Ga2dC/VthSUR2tlR73cTg36Q1Q2EHHan66OV8+u5K7JQjfTWcIubBxu3uFR MXkyCcLjvYD3p9JN20iLGsxs39ObWSVR7KReygl1yDV8cvPeleN/0jcOUnvpJ8Le FeLWTg2p+8kgpBG5MV3lmijfeP8KxGqDuo3Md9LBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=011gI+ BgGTRjW9meQFSGPKOwc0Q8Xk0xnexizoH14qo=; b=eRyXH8ogy1fKuF2Y6mJ4C2 VYx1J2sqzcW84ona10jmJNHeFZ70+KEF9b5Qn9DSyr2kCky28StV9b5KwhX44KZS RiLLWl5KPM7cDgQSziF/NkvTp5A0eDeRCpqJrJgd5BCpSeHW/g6kfSjMClWgOOVJ AL+lxutUmVbdefDYZCf8ua13LPdYoj+BMc5V9XdUghvQCplDEEYvlSFwM0VV+MJg TWEoaYM8mD/FbeMiw63iqjea0GN6Gk3Z3pbj/14xiXjfgzNGae5HWTRsLS5g0qAF qdSr0VTrQmwInftHfggZajqolJVatNGE9VSC7PSZ+YLQ57+4em4PmeIwu1NspNOA == X-ME-Proxy: X-ME-Sender: Received: from ranger.suse (74-93-105-186-washington.hfc.comcastbusiness.net [74.93.105.186]) by mail.messagingengine.com (Postfix) with ESMTPA id DACA0E455B; Thu, 5 Jul 2018 13:18:18 -0400 (EDT) Subject: Re: Split daterange into sub periods To: Hellmuth Vargas , andreas@a-kretschmer.de Cc: pgsql-general@lists.postgresql.org References: <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de> From: Adrian Klaver Message-ID: <04e5951f-e2ee-7594-3022-3251118ccf0d@aklaver.com> Date: Thu, 5 Jul 2018 10:18:18 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 07/05/2018 08:30 AM, Hellmuth Vargas wrote: > > Hi > > select ($$[$$|| to_char(min(n.dato),'YYYY-MM-DD') || $$,$$ || > to_char(max(n.dato),'YYYY-MM-DD') || $$]$$)::daterange, > daterange(min(n.dato)::date,max(n.dato)::date) > from ( > select u.dato,anterior,(u.dato-anterior)::interval,sum(case when > anterior is null or (u.dato -anterior)::interval='1 day'::interval  then > 0 else 1 end) over(order by u.dato) as grupo > from ( > select u.dato, lag(u.dato) over( order by u.dato) as anterior, > lead(u.dato) over( order by u.dato) > from ( > select * from generate_series(lower('[2018-01-01, > 2018-01-31]'::daterange),upper('[2018-01-01, 2018-01-31]'::daterange),'1 > day'::interval) as a(dato) > except > ( > select generate_series(lower(a.dato),upper(a.dato),'1 day'::interval) > from > (values('[2018-01-04,2018-01-06]'::daterange),('[2018-01-09,2018-01-12]'::daterange),('[2018-01-18,2018-01-19]'::daterange)) > as a(dato) > > ) > ) as u order by u.dato > ) as u > ) as n > group by grupo > order by 1 > > > >         daterange        |        daterange > -------------------------+------------------------- >  [2018-01-01,2018-01-04) | [2018-01-01,2018-01-03) >  [2018-01-08,2018-01-09) | empty >  [2018-01-14,2018-01-18) | [2018-01-14,2018-01-17) >  [2018-01-21,2018-02-02) | [2018-01-21,2018-02-01) > (4 rows) > Interesting but I am not sure this is working as the OP wants. If I am following the excluded ranges from your query are: [2018-01-04,2018-01-06] [2018-01-09,2018-01-12] [2018-01-18,2018-01-19] From what I understand the OP wants, the returned periods should be: [2018-01-01,2018-01-03] [2018-01-07,2018-01-08] [2018-01-13,2018-01-17] [2018-01-20,2018-01-31] -- Adrian Klaver adrian.klaver@aklaver.com