pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: hmidi slim <hmidi.slim2@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Split daterange into sub periods
Date: Thu, 5 Jul 2018 07:03:31 -0700
Message-ID: <21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com> (raw)
In-Reply-To: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com>
References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com>

On 07/05/2018 06:49 AM, 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]

Overlapping what?
They are not overlapping each other.

> 
> 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]

Not sure what the above represents.
Are you looking for the dates in the range [2018-01-01, 2018-01-31] that 
are not in the 3 sub-ranges at the top of the post?

> 
> 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

That is expected:

https://www.postgresql.org/docs/10/static/functions-range.html

"The union and difference operators will fail if the resulting range 
would need to contain two disjoint sub-ranges, as such a range cannot be 
represented."

> 
> *
> 
> Is there any operators to make the split of daterange?*
> *

Not that I know of.

> 
> 
> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (10+ messages)  latest in thread

Message-ID: <21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com>
Permalink:  ../21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com/
Also on:    postgresql.org/message-id/21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com

 · 

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: adrian.klaver@aklaver.com, hmidi.slim2@gmail.com
  Subject: Re: Split daterange into sub periods
  In-Reply-To: <21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com>
  References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com> <21b62dd6-fd29-b0da-8b0d-89c04685d97e@aklaver.com>

* 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