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 1lxlbw-00050i-Cd for pgsql-general@arkaria.postgresql.org; Mon, 28 Jun 2021 07:23:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lxlbt-00088D-K5 for pgsql-general@arkaria.postgresql.org; Mon, 28 Jun 2021 07:23:13 +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 1lxlbt-000885-8W for pgsql-general@lists.postgresql.org; Mon, 28 Jun 2021 07:23:13 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lxlbo-0001hq-Nw for pgsql-general@postgresql.org; Mon, 28 Jun 2021 07:23:12 +0000 Received: by mail-ej1-x635.google.com with SMTP id bu12so28339986ejb.0 for ; Mon, 28 Jun 2021 00:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=TYPZ658CilYkb+ZYyAhcEtJwq4Sr8sa9MT/FIvvugps=; b=qRKE4S3EZgwow5BxvCgv/afeSFXVYkwD3z6j1era/p9hyUviQmd3tL6sW3xGZ4ccEF KxK49XBJb3hxaf33D5yaL8jecFdNbK93Fju9ea3T166NX0Bg3NVk1bRXooiJRdMyFmSY h+hSewuE94icOhMZaZJ2vld9P0mzlAFeTpz8rjlodCN++1PfsOHyZNZyWhq0+fUzF4yS QYmVOgFbHkzwTJ9VIW/OtmO2MdyxuTJGRirTWXkN81DeaXrHOwnR1VoGedDz9XJm8x5S 8lqUOWdnDdCI3GaiTILhX8JKFKBh4h5khCniX7lS1Dcx2fMgVs2Dm18gQLHUcYCrwFwt 1R0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=TYPZ658CilYkb+ZYyAhcEtJwq4Sr8sa9MT/FIvvugps=; b=Pn6Z0FXGzspGqbIhxaFeuaItYS88zyllwQ+sZ97VDb49uH2sJgnacWkcWgU4A18Q94 NNLBacyZ2o36FvJLQhEqA+n6qAxldf14gMQcIV11Sk/lJCFT8lmciQmTF9JDAXONmvnT mS8UWX/3kc0bMeL0nvValtUkMoHl2fnQXd7YBJOjS9HidkftTCuqx3WrwgiwelM6SNde jIBeKh6Vli9ekczhMBaYY6XEo6NTcc/sQu/dsaZBDBALNPZgBqFUxcIZgBtlMpq9xbh4 qdJ1nS8QCzCiqv4Pzgz32PfBWNATHAOtzCTltA+5mrel7X7Q/SUjH3Xss/ANt1J1LcSd rDqA== X-Gm-Message-State: AOAM532o9WsEHyDaKR0SykNXuQnKf62ZMGczLRssXYTErvhCdq63NQuq FxvAncPC8XTJOGmJvm131Ns= X-Google-Smtp-Source: ABdhPJynGQeGdbeAf9OWrZmPTT91qUytHH1Qior658Hah+Ss80Oqt0ePWEGpzHgTqhJq3NbctS7faA== X-Received: by 2002:a17:906:ca4a:: with SMTP id jx10mr23043697ejb.200.1624864987608; Mon, 28 Jun 2021 00:23:07 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id u21sm6446517eja.59.2021.06.28.00.23.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jun 2021 00:23:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Overlapping timestamptz ranges with priority From: Alban Hertroys In-Reply-To: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> Date: Mon, 28 Jun 2021 09:23:06 +0200 Cc: PostgreSQL Content-Transfer-Encoding: quoted-printable Message-Id: <2FAFBA73-C7C1-4545-9EDD-3FA71D879DD7@gmail.com> References: <726445f7-ff72-9412-2e6d-2badcc58f17e@rodonnell.ie> To: Ray O'Donnell X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 28 Jun 2021, at 0:41, Ray O'Donnell wrote: >=20 > Hi all, >=20 (=E2=80=A6) > create table bookings ( > booking_id bigint not null, > booking_time tstzrange not null, >=20 > constraint bookings_pk primary key (booking_id) > ); >=20 > 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', '[)')); >=20 >=20 > And what I'd like to be able to do is pull out the following: >=20 >=20 > 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.