From: Ray O'Donnell <ray@rodonnell.ie>
To: PostgreSQL <pgsql-general@postgresql.org>
Subject: Overlapping timestamptz ranges with priority
Date: Sun, 27 Jun 2021 23:41:09 +0100
Message-ID: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> (raw)
Hi all,
I'm playing with timestamptz ranges for a hobby project. I have a table
with a tstzrange column, in which the timestamps can overlap; where they
do, rows with a higher priority (derived from a bigint primary key
column) should be picked.
What I'd like to do is present a view which shows timestamp ranges at
the front of the queue, as it were; where ranges overlap, these may be
segments of a range from a particular row. I'm having trouble with this
and would appreciate suggestions.
Here's a slightly simplified example:
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")
As you can see, where the two slots overlap (between 13:00 and 14:00),
the booking with the higher priority (lower booking ID) takes precedence.
The closest I've got to it is this:
select a.booking_id, a.booking_time - b.booking_time from bookings a
inner join bookings b on (a.booking_id < b.booking_id)
union
select d.booking_id, d.booking_time - c.booking_time from bookings d
inner join bookings c on (d.booking_id > c.booking_id)
This gives me:
booking_id | ?column?
------------+-----------------------------------------------------
1 | ["2021-06-20 12:00:00+01","2021-06-20 13:00:00+01")
2 | ["2021-06-20 14:00:00+01","2021-06-20 16:00:00+01")
...which is missing the bit where they overlap; and anyway, when I add
in more bookings, it gives me nonsense results. :-)
Any pointers will be greatly appreciated!
Many thanks in advance,
Ray.
--
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie
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: ray@rodonnell.ie
Subject: Re: Overlapping timestamptz ranges with priority
In-Reply-To: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie>
References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie>
* 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