Re: SQL help:
Re: SQL help:
Hey Ray,On Tue, Aug 11, 2026 at 2:33 PM Ray O'Donnell <ray@rodonnell.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@rodonnell.ie