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 8675873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:03:24+00:00 2026-06-12T20:03:24+00:00

Say there is a table which stores a hierarchical structure like this: item_id |

  • 0

Say there is a table which stores a hierarchical structure like this:

item_id | hierarchical_id 
--------+-----------------
    1   | ;1;
    2   | ;1;2;
    3   | ;1;2;3;
    4   | ;1;2;4;
    5   | ;1;2;4;5;

The hierarchy stored here is 1 as root, 2 is a child of 1, 3 and 4 are children of 2 and 5 is the child of 4.

The query

SELECT
  -- the substr is used to remove the first and last semicolumns
  regexp_split_to_table(substr(hierarchical_id, 2, length(hierarchical_id) - 2)
                        , E';'
  ) as parent_id,
  item_id,
  hierarchical_id
FROM 
  table

returns

parent_id | item_id | hierarchical_id
----------+---------+-----------------
       1  |    1    | ;1;
       1  |    2    | ;1;2;
       2  |    2    | ;1;2;
       1  |    3    | ;1;2;3;
       3  |    3    | ;1;2;3;
       1  |    4    | ;1;2;3;
       2  |    4    | ;1;2;4;
       4  |    4    | ;1;2;4;
       1  |    5    | ;1;2;4;5;
       2  |    5    | ;1;2;4;5;
       4  |    5    | ;1;2;4;5;
       5  |    5    | ;1;2;4;5;

How can I modify the query to get a 4th column like this:

parent_id | item_id | hierarchical_id | distance
----------+---------+-----------------+---------
       1  |    1    | ;1;             | 0
       1  |    2    | ;1;2;           | 1
       2  |    2    | ;1;2;           | 0
       1  |    3    | ;1;2;3;         | 2 
       2  |    3    | ;1;2;3;         | 1
       3  |    3    | ;1;2;3;         | 0
       1  |    4    | ;1;2;4;         | 2
       2  |    4    | ;1;2;4;         | 1
       4  |    4    | ;1;2;4;         | 0
       1  |    5    | ;1;2;4;5;       | 3
       2  |    5    | ;1;2;4;5;       | 2
       4  |    5    | ;1;2;4;5;       | 1
       5  |    5    | ;1;2;4;5;       | 0

The meaning of distance is the distance between the item_id and the parent_id on the current row. Eg: the distance between a node and itself is 0, and the distance between a node and its parent is 1, the distance between a node and its parent parent is 2 etc. It does not have to start at 0.

row_number would work fine if I could get it to restart at 0 for each group of equal item_ids, since the ids in hierarchical_id are ordered.

Any suggestions?

  • 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-12T20:03:25+00:00Added an answer on June 12, 2026 at 8:03 pm

    Window functions give you lots of control; see 4.2.8. Window Function Calls.

    The key thing you need is:

    row_number() OVER (PARTITON BY item_id ORDER BY hierarchical_id)
    

    Given data:

    create table t ( item_id integer, hierarchical_id text );
    insert into t (item_id, hierarchical_id) values
    (1,';1;'),
    (2,';1;2;'),
    (3,';1;2;3;'),
    (4,';1;2;4;'),
    (5,';1;2;4;5;');
    

    the query:

    WITH x AS (
      SELECT regexp_split_to_table(substr(hierarchical_id, 2, length(hierarchical_id) - 2), E';') as parent_id,
        item_id,
        hierarchical_id
      FROM t
    )
    SELECT 
      *,
      row_number() OVER (PARTITION BY item_id ORDER BY parent_id DESC) - 1 AS distance
    FROM x
    ORDER BY item_id, parent_id;
    

    produces:

     parent_id | item_id | hierarchical_id | distance 
    -----------+---------+-----------------+----------
     1         |       1 | ;1;             |        0
     1         |       2 | ;1;2;           |        1
     2         |       2 | ;1;2;           |        0
     1         |       3 | ;1;2;3;         |        2
     2         |       3 | ;1;2;3;         |        1
     3         |       3 | ;1;2;3;         |        0
     1         |       4 | ;1;2;4;         |        2
     2         |       4 | ;1;2;4;         |        1
     4         |       4 | ;1;2;4;         |        0
     1         |       5 | ;1;2;4;5;       |        3
     2         |       5 | ;1;2;4;5;       |        2
     4         |       5 | ;1;2;4;5;       |        1
     5         |       5 | ;1;2;4;5;       |        0
    

    which looks roughly right, but since your expected output doesn’t seem to match the output of the query you provided when I run it (Pg 9.1) it’s hard to know for sure.

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

Sidebar

Related Questions

Let's say there is this table which stores the number of visitors for each
I have a MYSQL table which stores teams. Table structure: CREATE TABLE teams (
Let's say you have a table which stores articles, and each article can have
Let's say there's a table created as follows: create table testTable ( colA int
Say I have a Person table with 200000 records, there's a clustered index on
I have a table say, ITEM, in MySQL that stores data as follows: ID
I have a MySQL Table that looks like this: The SQL to create the
(Note: this is for MS SQL Server) Say you have a table ABC with
So here's the deal. I've designed as schema which stores the daily stock quotes
I would like the users be able to create their accounts (which stores the

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.