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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:20:05+00:00 2026-06-14T00:20:05+00:00

I am trying to do something that should be simple but I’m missing something.

  • 0

I am trying to do something that should be simple but I’m missing something. I am trying to return some hierarchical data in grouped and ordered. a Rather than trying to explain, I am attaching a picture… heard they are worth a thousand words:

Order of data in the database vs the order the resultset needs to be in.

Note that hard coded values are no good because the main selection criteria will be the article_id, and then ordered along the lines of the first ‘root level’ article_comment_id for the article followed by its sub-nodes, then the next ‘root level’ article_comment_id and its sub-nodes.

I am providing a sample table and data so you can see what I mean, plus a screen capture showing what I am trying to achieve.

CREATE TABLE test_comment
(
  article_comment_id bigserial NOT NULL,
  article_id bigint NOT NULL,
  parent_comment_id bigint,
  comment text NOT NULL,
  comment_depth integer,
  CONSTRAINT test_comment_pkey PRIMARY KEY (article_comment_id )
)

INSERT INTO test_comment (article_comment_id, article_id, parent_comment_id, comment, comment_depth)
VALUES
 (1, 100, 0, 'First Root Comment', 0)
,(5, 100, 0, 'Second Root Comment', 0)
,(2, 100, 1, 'Reply 1 to Root Comment', 1)
,(3, 100, 2, 'Reply 2 to Reply 1', 2)
,(4, 100, 3, 'Reply 3 to Reply 2', 3)
,(6, 100, 2, 'Reply 4 to Reply 1', 2)
,(7, 100, 5, 'Reply 5 to Second Root Comment', 1);

I have tried this, but it does not provide the proper order:

with recursive comment_list(article_comment_id, parent_comment_id, comment, article_id) AS (
    select c.article_comment_id, c.parent_comment_id, c.comment, c.article_id
    from test_comment c
    where article_id = 100
  union
    select c.article_comment_id, c.parent_comment_id, c.comment, c.article_id
    from test_comment c, comment_list cl
    where c.article_comment_id = cl.article_comment_id
    )
    select * from comment_list;

And can this PostgreSQL specific query be used with JPA, anyway?

  • 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-14T00:20:06+00:00Added an answer on June 14, 2026 at 12:20 am

    You need to “carry” the root article id down to each node and calculate the hierarchy level to be able to get that ordering:

    with recursive comment_list(article_comment_id, parent_comment_id, comment, article_id, level, root_id) AS (
        select c.article_comment_id, 
               c.parent_comment_id, 
               c.comment, 
               c.article_id, 
               0 as level,
               article_comment_id as root_id 
        from test_comment c
        where article_id = 100
          and parent_comment_id = 0
    
      union
    
        select c.article_comment_id, 
               c.parent_comment_id, 
               c.comment, 
               c.article_id, 
               cl.level + 1,
               cl.article_comment_id 
        from test_comment c
           join comment_list cl on c.parent_comment_id = cl.article_comment_id
    )
    select article_comment_id, 
         parent_comment_id, 
         comment, 
         article_id
    from comment_list
    order by root_id, level;
    

    SQLFiddle: http://www.sqlfiddle.com/#!12/af0d6/4

    (Btw: you have your join wrong in the recursive part of the union)

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

Sidebar

Related Questions

I'm trying to do something that I think should be fairly simple but I've
I'm trying to do something that seems like it should be very simple, but
I am trying to do something that should be fairly simple but ISNULL isn't
I'm trying to do something that should be very simple, but it's causing this
I am trying to do something that should be really simple but I am
I'm trying to do something that should be real simple but I'm getting errors
I'm trying to do something that seems like it should be very simple, but
I'm trying to do something that should be pretty simple - but it's doing
I am trying to do something that should be simple, but think I am
I'm trying to achieve something that should be fairly simple, but it doesn't work.

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.