Skip to content

Instantly share code, notes, and snippets.

@mayhem
Created July 25, 2020 22:35
Show Gist options
  • Save mayhem/862ce3631e44d4414d770b2629e15bf8 to your computer and use it in GitHub Desktop.
Save mayhem/862ce3631e44d4414d770b2629e15bf8 to your computer and use it in GitHub Desktop.
WITH RECURSIVE area_descendants AS (
SELECT entity0 AS parent, entity1 AS descendant, 1 AS depth
FROM l_area_area laa
JOIN link ON laa.link = link.id
WHERE link.link_type = 356
AND entity1 IN (13703, 81)
UNION
SELECT entity0 AS parent, descendant, (depth + 1) AS depth
FROM l_area_area laa
JOIN link ON laa.link = link.id
JOIN area_descendants ON area_descendants.parent = laa.entity1
WHERE link.link_type = 356
AND entity0 != descendant
)
SELECT iso.code
FROM area_descendants ad
JOIN iso_3166_1 iso ON iso.area = ad.parent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment