pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Ray O'Donnell <ray@rodonnell.ie>
Cc: PostgreSQL <pgsql-general@postgresql.org>
Subject: Re: Overlapping timestamptz ranges with priority
Date: Mon, 28 Jun 2021 09:23:06 +0200
Message-ID: <2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.com> (raw)
In-Reply-To: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie>
References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie>


> On 28 Jun 2021, at 0:41, Ray O'Donnell <ray@rodonnell.ie> wrote:
> 
> Hi all,
> 

(…)

> create table bookings (
>    booking_id bigint not null,
>    booking_time tstzrange not null,
> 
>    constraint bookings_pk primary key (booking_id)
> );
> 
> insert into bookings (booking_id, booking_time) values
> (1, tstzrange('2021-06-20 12:00+01', '2021-06-20 14:00+01', '[)')),
> (2, tstzrange('2021-06-20 13:00+01', '2021-06-20 16:00+01', '[)'));
> 
> 
> And what I'd like to be able to do is pull out the following:
> 
> 
> booking_id |                    slot_time
> ------------+-----------------------------------------------------
>          1 | ["2021-06-20 12:00:00+01","2021-06-20 14:00:00+01")
>          2 | ["2021-06-20 14:00:00+01","2021-06-20 16:00:00+01")


You could probably achieve this by using window function lag() over (order by booking_id), in combination with a case statement when the range from the previous row overlaps the current range.

That would only solve the case for immediately subsequent rows though, if you have multiple rows overlapping you will need to track the first range in that list.

Another possible route is a recursive CTE, with a similar approach.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (15+ messages)  latest in thread

Message-ID: <2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.com>
Permalink:  ../2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.com/
Also on:    postgresql.org/message-id/2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.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: haramrae@gmail.com, ray@rodonnell.ie
  Subject: Re: Overlapping timestamptz ranges with priority
  In-Reply-To: <2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.com>
  References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> <2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.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