Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:10:44+00:00 2026-06-09T04:10:44+00:00

ENV : postgresql-8.4 I’m trying to build a category tree . Basically I’m expecting

  • 0

ENV : postgresql-8.4

I’m trying to build a category tree . Basically I’m expecting a final output such :

categoryName 
categoryPath
leafcategory

e.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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:10:45+00:00Added an answer on June 9, 2026 at 4:10 am

    It appears you were joining on the wrong field.

     --  create some test data
    DROP SCHEMA tmp CASCADE;
    CREATE SCHEMA tmp ;
    SET search_path=tmp;
    
    CREATE TABLE categories
        -- ( id  SERIAL PRIMARY KEY
        ( categoryid SERIAL PRIMARY KEY
        , categoryparentid bigint REFERENCES categories(categoryid)
        , categoryname text
        -- , status integer DEFAULT 0
        -- , lang text
        -- , ebaysiteid text
        -- , country text
        -- , tempid text
        -- , leafcategory boolean
            );
    INSERT INTO categories(categoryid,categoryparentid) SELECT gs, 1+(gs/6)::integer
    FROM generate_series(1,50) gs;
    
    UPDATE categories SET categoryname = 'Name_' || categoryid::text;
    UPDATE categories SET categoryparentid = NULL WHERE categoryparentid <= 0;
    UPDATE categories SET categoryparentid = NULL WHERE categoryparentid  >= categoryid;
    
    
    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.categoryid  = c.categoryparentid
        )
    SELECT * FROM tree ORDER BY category_tree;
    

    EDIT: the other (“non-function”) notation for recursive seems to work better:

    WITH RECURSIVE tree AS (
        SELECT
            categoryparentid AS parent
            , categoryid AS self
            , categoryname AS treepath
            , 0 AS depth
        FROM categories
        WHERE categoryparentid IS NULL
    UNION ALL
        SELECT
            c.categoryparentid AS parent
            , c.categoryid AS self
            , t.treepath  || '/' || c.categoryname AS treepath
            , depth+1 AS depth
        FROM categories c
        JOIN tree t ON t.self  = c.categoryparentid
        )
    SELECT * FROM tree ORDER BY parent,self
       ;
    

    UPDATE: in the original query, you should replace

    WHERE CategoryParentID IS NULL
    

    by:

    WHERE CategoryParentID = 0
    

    or maybe even:

    WHERE COALESCE(CategoryParentID, 0) = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Env: OS: feodra 16 haskell-platform wxGTK-devel ghc 7.0.4 I am trying to install wxHaskell
Is it possible to access Rails.env inside Sass files? I'm trying to do it
My env: rvm ruby 1.9.2 , and I build ImageMagick 6.7.4-6 from source. When
Can you use an env var expansion such as %APPDATA% in log4j.properties file? I
Env.: VS2008 C# project I need to build my app for use in 2
I need to set up the following env variables such that I can a
I am trying to switch my Rails TEST environment from SQLite3 to Postgresql. However,
#!/usr/bin/env perl use warnings; use strict; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
I can't access env variables in the Rails console, while in the application they
!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.