Skip to content

Instantly share code, notes, and snippets.

@baffioso
Last active August 16, 2018 07:49
Show Gist options
  • Save baffioso/a5ad42de75fee95b8cd7eb9d14b4da82 to your computer and use it in GitHub Desktop.
Save baffioso/a5ad42de75fee95b8cd7eb9d14b4da82 to your computer and use it in GitHub Desktop.
Spidsbelastning med overlab i biler der holder stille. Kilder: https://stackoverflow.com/a/48648943
select bilreg, pnavn, gyldighed * tstzrange('2018-07-04 09:00:00+02', '2018-07-04 16:00:00+02', '[]') gyldighed, hjemme, geom
from LORA_FLAADESTYRING.BIL_PARKERING_PERIODE
where (PNAVN <> '*udenfor*' and BILREG <> '*mangler*' and hjemme = TRUE) AND
GYLDIGHED && tstzrange('2018-07-04 09:00:00+02', '2018-07-04 16:00:00+02', '[]')
order by bilreg, lower(GYLDIGHED);
WITH RECURSIVE recRange AS
(
SELECT id,
range,
CAST(id as varchar(100)) as path,
1 as depth
FROM drrange
UNION ALL
SELECT drrange.id,
drrange.range * recRange.range,
CAST(recrange.path || ',' || drrange.id AS VARCHAR(100)),
recRange.depth + 1
FROM recRange INNER JOIN drrange ON
recRange.range && drrange.range
AND recRange.id < drrange.id
--Prevent cycling (which shouldn't happen with that join)
--WHERE depth < 150
),
drrange AS
(
select bilreg as id, PNAVN, gyldighed * tstzrange('2018-07-04 09:00:00+02', '2018-07-04 16:00:00+02', '[]') AS range
from LORA_FLAADESTYRING.BIL_PARKERING_PERIODE
where (PNAVN <> '*udenfor*' and BILREG <> '*mangler*' and hjemme = TRUE) AND
GYLDIGHED && tstzrange('2018-07-04 09:00:00+02', '2018-07-04 16:00:00+02', '[]')
)
SELECT path as bilreg, range as overlap, DEPTH antal FROM recRange t1
WHERE depth > 0
-- Keep the range only if it is NOT contained by
--+ any other range that has a deeper depth then it
--+ and has the same ending id (id). This isn't the
--+ best logic and could make false positives, but...
--+ it's in the ballpark
AND NOT range <@ ANY(SELECT range FROM recRange WHERE depth > 1 AND depth > t1.depth AND id = t1.id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment