Skip to content

Instantly share code, notes, and snippets.

@szymonk92
Last active July 31, 2019 10:13
Show Gist options
  • Save szymonk92/b5e86d4ffb3c07fa05ddd8e9635b7b47 to your computer and use it in GitHub Desktop.
Save szymonk92/b5e86d4ffb3c07fa05ddd8e9635b7b47 to your computer and use it in GitHub Desktop.
WITH lvl1 AS
(SELECT dir_id , 1 AS Lvl
FROM t_directory AS tart1
WHERE tart1.parentDirectory = 201
)
,
lvl2 AS
(
SELECT dir_id, 2 AS Lvl
FROM t_directory AS tart2
WHERE tart2.parentDirectory IN (SELECT dir_id FROM lvl1)
),
lvl3 AS
(
SELECT dir_id, 3 AS Lvl
FROM t_directory AS tart3
WHERE tart3.parentDirectory IN (SELECT dir_id FROM lvl2)
)
SELECT dir_id, Lvl FROM lvl1
UNION
SELECT dir_id, Lvl FROM lvl2
UNION
SELECT dir_id, Lvl FROM lvl3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment