From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Hellmuth Vargas <hivs77@gmail.com>
To: andreas@a-kretschmer.de
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Split daterange into sub periods
Date: Thu, 5 Jul 2018 10:18:18 -0700
Message-ID: <04e5951f-e2ee-7594-3022-3251118ccf0d@aklaver.com> (raw)
In-Reply-To: <CAN3Qy4o9SGpyqWbkwAWT53fBGqA0U6dS7XW9aRNR3eT9ExE4WQ@mail.gmail.com>
References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com>
<4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de>
<CAN3Qy4o9SGpyqWbkwAWT53fBGqA0U6dS7XW9aRNR3eT9ExE4WQ@mail.gmail.com>
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
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, hivs77@gmail.com, andreas@a-kretschmer.de
Subject: Re: Split daterange into sub periods
In-Reply-To: <04e5951f-e2ee-7594-3022-3251118ccf0d@aklaver.com>
References: <CAMsqVxsSiN_-Tx3tPzAammUyQNBVOKY3cpHfdGFb8wVX7oK3+w@mail.gmail.com> <4fafc5e2-eba4-61c9-c6d0-9f785be4366e@a-kretschmer.de> <CAN3Qy4o9SGpyqWbkwAWT53fBGqA0U6dS7XW9aRNR3eT9ExE4WQ@mail.gmail.com> <04e5951f-e2ee-7594-3022-3251118ccf0d@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