ENV : postgresql-8.4
I'm trying to build a category tree . Basically I'm expecting a final output such :
categoryName categoryPath leafcategorye.g. :
Digital Camera Electronics ::: Digital Camera true
The table structure is
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
categoryid bigint,
categoryparentid bigint,
categoryname text,
status integer DEFAULT 0,
lang text,
eysiteid text,
country text,
tempid text,
leafcategory boolean
);
So far I've got this but is not working. Any help would be highly appreciated :
WITH RECURSIVE tree (CategoryID, CategoryParentID, CategoryName, category_tree, depth)
AS (
SELECT
CategoryID,
CategoryParentID,
CategoryName,
CategoryName AS category_tree,
0 AS depth
FROM categories
WHERE CategoryParentID IS NULL
UNION ALL
SELECT
c.CategoryID,
c.CategoryParentID,
c.CategoryName,
tree.category_tree || '/' || c.CategoryName AS category_tree,
depth+1 AS depth
FROM tree
JOIN categories c ON (tree.category_tree = c.CategoryParentID)
)
SELECT * FROM tree ORDER BY category_tree;
Sample from database
cat=> select * from categories;
id | categoryid | categoryparentid | categoryname | status | lang | eysiteid | country | tempid | leafcategory
-------+------------+------------------+--------------------------------+--------+------+------------+---------+--------+--------------
1 | -1 | 0 | Root | 1 | en | 0 | us | | f
2 | 20081 | -1 | Antiques | 1 | en | 0 | us | | f
17 | 1217 | 20081 | Primitives | 0 | en | 0 | us | | t
23 | 22608 | 20081 | Reproduction Antiques | 0 | en | 0 | us | | t
24 | 12 | 20081 | Other | 0 | en | 0 | us | | t
25 | 550 | -1 | Art | 1 | en | 0 | us | | f
29 | 2984 | -1 | Baby | 1 | en | 0 | us | | f