pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Kretschmer <andreas@a-kretschmer.de>
To: pgsql-general@lists.postgresql.org
Subject: Re: Split daterange into sub periods
Date: Thu, 5 Jul 2018 17:39:26 +0200
Message-ID: <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de> (raw)
In-Reply-To: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com>
References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com>



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

view thread (10+ messages)  latest in thread

Message-ID: <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de>
Permalink:  ../4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de/
Also on:    postgresql.org/message-id/4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andreas@a-kretschmer.de
  Subject: Re: Split daterange into sub periods
  In-Reply-To: <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de>
  References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com> <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox