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

Ask A Question

Stats

  • Questions 520k
  • Answers 520k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about solving your problem this way: try: f.write(msg) except… May 16, 2026 at 8:44 pm
  • Editorial Team
    Editorial Team added an answer The filter won't be invoked when: The filter class is… May 16, 2026 at 8:44 pm
  • Editorial Team
    Editorial Team added an answer You seem to be answering your own question. It is… May 16, 2026 at 8:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i have this problem to find a particular xml node l have post this
I have a list (the paths) of images saved locally. How can I find
Which algorithm do you recommend to find out the nearest node which can be
This question could easily take multiple paths, so I will hit the more specific
I have a list of possible paths to use for a default input data
The situation: I need to convert our current development environment from Windows XP 32-bit to
I am not sure if there's an answer for this already -- couldn't find
I have the following output structure of executables in my solution: %ProgramFiles% | +-[MyAppName]
I am currently writing a quick script in Ruby that goes through all the
Lately I have been working on OS X. Things were going pretty peachy for

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.