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 1fb6M1-0007vz-9a for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 15:39:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fb6Lz-0007nE-M5 for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 15:39:31 +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 1fb6Lz-0007n2-Dj for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 15:39:31 +0000 Received: from mail.i-mark.de ([188.138.104.222]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fb6Lw-00055r-Fv for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 15:39:30 +0000 Received: from [10.0.0.160] (80-80-245-160.stat.stw-net.at [80.80.245.160]) by mail.i-mark.de (Postfix) with ESMTPSA id C916415803BE for ; Thu, 5 Jul 2018 17:39:22 +0200 (CEST) Authentication-Results: loft7616.startdedicated.de; spf=pass (sender IP is 80.80.245.160) smtp.mailfrom=andreas@a-kretschmer.de smtp.helo=[10.0.0.160] Received-SPF: pass (loft7616.startdedicated.de: connection is authenticated) Subject: Re: Split daterange into sub periods To: pgsql-general@lists.postgresql.org References: From: Andreas Kretschmer Message-ID: <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de> Date: Thu, 5 Jul 2018 17:39:26 +0200 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: multipart/alternative; boundary="------------81BEE3D1C4DD9DA94CBF03AC" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------81BEE3D1C4DD9DA94CBF03AC Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 05.07.2018 15:49, hmidi slim wrote: > Hi, > I'm looking for splitting a daterange into many subperiods following > this example: > > Base Date: [2018-01-01, 2018-01-31] > overlapped_periods: > 1- [ 2018-01-04, 2018-01-06] > 2- [ 2018-01-09, 2018-01-12] > 3- [ 2018-01-18, 2018-01-19] > > I try to get such a result: > 1- [ 2018-01-01, 2018-01-03] > 2- [ 2018-01-07, 2018-01-08] > 3- [ 2018-01-13, 2018-01-17] > 4- [ 2018-01-20, 2018-01-31] > > The operator '-' does not support this : > SELECT daterange('2018-01-01', '2018-01-31', '[]') - > daterange('2018-01-04', '2018-01-06', '[]'); > I got this error: > *ERROR: result of range difference would not be contiguous * > Is there any operators to make the split of daterange? andreas@[local]:5432/test# \d hmidi                 Table "public.hmidi"  Column |   Type    | Collation | Nullable | Default --------+-----------+-----------+----------+---------  id     | integer   |           | not null |  d      | daterange |           |          | Indexes:     "hmidi_pkey" PRIMARY KEY, btree (id) andreas@[local]:5432/test# insert into hmidi values (1,'[2018-01-04,2018-01-06]');INSERT 0 1 andreas@[local]:5432/test# insert into hmidi values (2,'[2018-01-09,2018-01-12]');INSERT 0 1 andreas@[local]:5432/test# insert into hmidi values (3,'[2018-01-18,2018-01-19]');INSERT 0 1 andreas@[local]:5432/test# with month as (select s::date from generate_series('2018-01-01'::date, '2018-01-31'::date,'1day'::interval) s), tmp as ( select month.s, case when hmidi.d @> month.s then 1 else NULL end as covered from month left join hmidi on month.s <@ hmidi.d),tmp2 as ( select *, coalesce((sum(case when covered = 1 then 1 else NULL end) over (order by s))+1,1) as p from tmp) select p, min(s), max(s) from tmp2 where covered is null group by p order by p;  p  |    min     |    max ----+------------+------------   1 | 2018-01-01 | 2018-01-03   4 | 2018-01-07 | 2018-01-08   8 | 2018-01-13 | 2018-01-17  10 | 2018-01-20 | 2018-01-31 (4 rows) Regards, Andreas -- 2ndQuadrant Deutschland --------------81BEE3D1C4DD9DA94CBF03AC Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit



On 05.07.2018 15:49, hmidi slim wrote:
Hi,
I'm looking for splitting a daterange into many subperiods following this example:

Base Date: [2018-01-01, 2018-01-31]
overlapped_periods:
1- [ 2018-01-04, 2018-01-06]
2- [ 2018-01-09, 2018-01-12]
3- [ 2018-01-18, 2018-01-19]

I try to get such a result:
1- [ 2018-01-01, 2018-01-03]
2- [ 2018-01-07, 2018-01-08]
3- [ 2018-01-13, 2018-01-17]
4- [ 2018-01-20, 2018-01-31]

The operator '-' does not support this :
SELECT daterange('2018-01-01', '2018-01-31', '[]') - daterange('2018-01-04', '2018-01-06', '[]');

I got this error:
ERROR: result of range difference would not be contiguous

Is there any operators to make the split of daterange?
andreas@[local]:5432/test# \d hmidi
                Table "public.hmidi"
 Column |   Type    | Collation | Nullable | Default
--------+-----------+-----------+----------+---------
 id     | integer   |           | not null |
 d      | daterange |           |          |
Indexes:
    "hmidi_pkey" PRIMARY KEY, btree (id)

andreas@[local]:5432/test# insert into hmidi values (1,'[2018-01-04,2018-01-06]');INSERT 0 1
andreas@[local]:5432/test# insert into hmidi values (2,'[2018-01-09,2018-01-12]');INSERT 0 1
andreas@[local]:5432/test# insert into hmidi values (3,'[2018-01-18,2018-01-19]');INSERT 0 1
andreas@[local]:5432/test# with month as (select s::date from generate_series('2018-01-01'::date, '2018-01-31'::date,'1day'::interval) s), tmp as ( select month.s, case when hmidi.d @> month.s then 1 else NULL end as covered from month left join hmidi on month.s <@ hmidi.d),tmp2 as ( select *, coalesce((sum(case when covered = 1 then 1 else NULL end) over (order by s))+1,1) as p from tmp) select p, min(s), max(s) from tmp2 where covered is null group by p order by p;
 p  |    min     |    max    
----+------------+------------
  1 | 2018-01-01 | 2018-01-03
  4 | 2018-01-07 | 2018-01-08
  8 | 2018-01-13 | 2018-01-17
 10 | 2018-01-20 | 2018-01-31
(4 rows)


Regards, Andreas
--
2ndQuadrant Deutschland
--------------81BEE3D1C4DD9DA94CBF03AC--