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

  • SEARCH
  • Home
  • 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 914841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:47:07+00:00 2026-05-15T17:47:07+00:00

How do I find all the paths from a specific node (node 36 in

  • 0

How do I find all the paths from a specific node (node 36 in the example)?

Let’s say we have two tables:

CATEGORIES      CATEG_PARENTS
id              idc  | idparent
--              ----------------
1               2      1
2               2      20
5               5      2
8               8      5
20              20     1
22              22     8
30              22     20
31              30     20
36              31     22
                31     30
                36     31

These are two possible representations:

alt text
(source: glopes at nebm.ist.utl.pt)

alt text
(source: glopes at nebm.ist.utl.pt)

This is the output I desire:

ids
-------------------
"{1,20,22,31}"
"{1,20,2,5,8,22,31}"
"{1,20,30,31}"
"{1,2,5,8,22,31}"

One path per line, written as an integer array.

(I’m going to write the answer I came up with, but I’ll accept any that’s simpler, if any)

  • 1 1 Answer
  • 1 View
  • 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-05-15T17:47:08+00:00Added an answer on May 15, 2026 at 5:47 pm
    WITH RECURSIVE parent_line(path, id) AS (
        SELECT ARRAY[(row_number() OVER (PARTITION BY CP.idc))::integer], C.id
        FROM categorias C JOIN categ_parents CP ON C.id = CP.idparent
        WHERE CP.idc = 36
        UNION ALL
        SELECT PL.path || (row_number() OVER (PARTITION BY PL.id))::integer,
            C.id
        FROM categorias C
        JOIN categ_parents CP ON C.id = CP.idparent
        JOIN parent_line PL on CP.idc = PL.id
    ),
    real_parent_line(path, chainid, id) AS (
        SELECT PL.path, (row_number() OVER (PARTITION BY PL.id)),
            PL.id
        FROM parent_line PL
        WHERE PL.id IN (
            SELECT id
            FROM categorias C LEFT JOIN categ_parents CP
                ON (C.id = CP.idc)
            WHERE CP.idc IS NULL
        )
        UNION ALL
        SELECT PL.path, chainid, PL.id
        FROM parent_line PL, real_parent_line RPL
        WHERE array_upper(PL.path,1) + 1 = array_upper(RPL.path,1)
            AND PL.path = RPL.path[1:(array_upper(RPL.path,1)-1)]
    )
    SELECT array_accum(id) AS ids
    FROM real_parent_line RPL
    GROUP BY chainid;
    

    The first WITH clause gives this:

    path             | id
    ------------------------
    "{1}"              31
    "{1,1}"            22
    "{1,2}"            30
    "{1,1,1}"          20
    "{1,1,2}"          8
    "{1,2,1}"          20
    "{1,1,2,1}"        5
    "{1,1,1,1}"        1
    "{1,2,1,2}"        1
    "{1,1,2,1,1}"      2
    "{1,1,2,1,1,1}"    1
    "{1,1,2,1,1,2}"    20
    "{1,1,2,1,1,2,1}"  1
    

    Thanks to #postgresql@freenode for some help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to find all paths between two vertices using QuickGraph? This is my custom
All this afte rupgrading from 1.4.1 I get a quite specific error message: Next
Let's say, for the sake of argument, that I have 30 asp.net web applications
I'm building a system in Node.js that is supposed to find all files in
I need to create an algorithm that could find all the critical paths in
I need some help with query from multiple tables. My database: I have following
I'm trying to find all element that contains all words in a phrase after
how can i find all the anchor tags with source pdf $string=hello this is
I need to find all the occurences of a file Test.TXT in c:\ or
I want to find all the demo words using PHP and regEx. $input ='demo';

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.