Created
February 14, 2016 03:11
-
-
Save badri/f6ec697983af7a4ae436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE genres_materialized_path AS ( | |
SELECT id, name, ARRAY[]::INTEGER[] AS path | |
FROM genres WHERE parent_id IS NULL | |
UNION ALL | |
SELECT genres.id, genres.name, genres_materialized_path.path || genres.parent_id | |
FROM genres, genres_materialized_path | |
WHERE genres.parent_id = genres_materialized_path.id | |
) SELECT * FROM genres_materialized_path WHERE 15 = genres_materialized_path.path[array_upper(genres_materialized_path.path,1)]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment