Re: SQL help:

From: Ray O'Donnell <ray(at)rodonnell(dot)ie>
To: John W Higgins <wishdev(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL help:
Date: 2026-08-12 11:48:32
Message-ID: 0102019ff5cd7d27-901fb6cd-ad22-40ec-9462-65b133a1d571-000000@eu-west-1.amazonses.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/08/2026 00:51, John W Higgins wrote:
> Hey Ray,
>
> On Tue, Aug 11, 2026 at 2:33 PM Ray O'Donnell <ray(at)rodonnell(dot)ie> wrote:
>
> Hi all,
>
> I need some help constructing a query...
>
> aircraft_reg |       slot_begin       |        slot_end        |
> booking_id | booking_priority | owner_uid
> --------------+------------------------+------------------------+------------+------------------+-----------
> EI-MCG       | 2026-08-22 09:00:00+01 | 2026-08-22 10:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 10:00:00+01 | 2026-08-22 11:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 11:00:00+01 | 2026-08-22 12:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 12:00:00+01 | 2026-08-22 13:00:00+01 |
>        217 |                1 | jbloggs
> EI-MCG       | 2026-08-22 12:00:00+01 | 2026-08-22 13:00:00+01 |
>        361 |                2 | rod
> EI-MCG       | 2026-08-22 13:00:00+01 | 2026-08-22 14:00:00+01 |
>        217 |                1 | jbloggs
> EI-MCG       | 2026-08-22 13:00:00+01 | 2026-08-22 14:00:00+01 |
>        361 |                2 | rod
> EI-MCG       | 2026-08-22 14:00:00+01 | 2026-08-22 15:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 15:00:00+01 | 2026-08-22 16:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 16:00:00+01 | 2026-08-22 17:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 17:00:00+01 | 2026-08-22 18:00:00+01 |
>        361 |                1 | rod
> EI-MCG       | 2026-08-22 18:00:00+01 | 2026-08-22 19:00:00+01 |
>        361 |                1 | rod
>
> So the key function for this work would be lag - it's a window
> function which allows you to "look back" x rows (default 1) and see
> its data.
>
> So lets start with a cte that tags new "groups"

<snip>

Fantastic, John - thanks very much for the detailed response! That does
what I need nicely. Much appreciated.

Kind regards,
Ray.

--

Ray O'Donnell // Galway // Ireland
ray(at)rodonnell(dot)ie

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ray O'Donnell 2026-08-12 11:52:31 Re: SQL help:
Previous Message John W Higgins 2026-08-11 23:51:11 Re: SQL help: