Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lxdSo-0004fw-RA for pgsql-general@arkaria.postgresql.org; Sun, 27 Jun 2021 22:41:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lxdSn-0007sE-Ln for pgsql-general@arkaria.postgresql.org; Sun, 27 Jun 2021 22:41:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lxdSn-0007s6-Aq for pgsql-general@lists.postgresql.org; Sun, 27 Jun 2021 22:41:17 +0000 Received: from srv01.teladesign.ie ([2001:41c9:1:41d::10]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lxdSk-0005wZ-Se for pgsql-general@postgresql.org; Sun, 27 Jun 2021 22:41:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rodonnell.ie; s=exim; h=Content-Transfer-Encoding:Content-Type:MIME-Version :Date:Message-ID:Subject:From:To:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Message-ID:In-Reply-To:References; bh=SmOZCD9BloMG+wTYnnAmi6MVEOT8nRedamRXdbvfNvE=; b=O884iC95FSrJiXK5iPVirAsACU XR149dyA08TFHjFf0mOz22y76rof/QAWT+ilTHAvVuLOtdzcsKixafIrBt1RKfD8t+DasA67KycF6 rglxKZ3Jq5dlkLDcVGSlsCFTsVZoRQwvfaAz9BMU0OZ0rND6NMA0WZRLu818GtVHBsHk=; Received: from [89.126.30.154] (helo=[192.168.15.118]) by srv01.teladesign.ie with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lxdSg-0008Da-AR for pgsql-general@postgresql.org; Sun, 27 Jun 2021 23:41:10 +0100 To: 'PostgreSQL' From: Ray O'Donnell Subject: Overlapping timestamptz ranges with priority Message-ID: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> Date: Sun, 27 Jun 2021 23:41:09 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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