Skip to content

Instantly share code, notes, and snippets.

@badri
Last active February 14, 2016 03:08
Show Gist options
  • Save badri/9f81560ceb9170397044 to your computer and use it in GitHub Desktop.
Save badri/9f81560ceb9170397044 to your computer and use it in GitHub Desktop.
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment