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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:01+00:00 2026-06-18T03:31:01+00:00

I need a hierarchical comments, and I need to sort by newest root comments.

  • 0

I need a hierarchical comments, and I need to sort by newest root comments.

I have this table

    CREATE TABLE comment
(
  id serial PRIMARY KEY,
  text text NOT NULL,
  parent_id integer REFERENCES comment(id),
  date timestamp
);

INSERT INTO comment (id, text, parent_id, date) VALUES (1, 'First Root Comment', NULL, '2013-01-02 20:00:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (2, 'Second Root Comment', NULL, '2013-01-02 20:20:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (3, 'Reply 1 to Root Comment', 1, '2013-01-02 20:01:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (4, 'Reply 2 to Reply 1', 1, '2013-01-02 20:02:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (5, 'Reply 1 to Second Root Comment', 2, '2013-01-02 20:21:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (6, 'Reply 3 to Reply 1', 1, '2013-01-02 20:03:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (7, 'Reply 1 to Reply 2', 4, '2013-01-02 20:02:30');

and this code to display hierarchical – http://www.sqlfiddle.com/#!12/96b37/2

WITH RECURSIVE cte (id, text, date, path, parent_id, depth)  AS (

    SELECT  id,
        text,
        date,
        array[id] AS path,
        parent_id,
        1 AS depth
    FROM    comment
    WHERE   parent_id IS NULL


    UNION ALL

    SELECT  comment.id,
        comment.text,
        comment.date,
        cte.path || comment.id,
        comment.parent_id,
        cte.depth + 1 AS depth
    FROM    comment
    JOIN cte ON comment.parent_id = cte.id
    )
    SELECT id, text, date, path, depth FROM cte
    ORDER BY path;

The result is that

Firt root comment (older root comment)
- Reply 1 to First Root Comment
- Reply 2 to First Root Comment
-- Reply 1 to Reply 2
- Reply 3 to First Root Comment
Second Root Comment (newest root comment)
- Reply 1 to Second Root Comment

But I want this result (sort by newest root comment)

Second Root Comment (newest root comment)
- Reply 1 to Second Root Comment
Firt root comment (older root comment)
- Reply 1 to First Root Comment
- Reply 2 to First Root Comment
-- Reply 1 to Reply 2
- Reply 3 to First Root Comment

Any idea? Thanks

  • 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-18T03:31:02+00:00Added an answer on June 18, 2026 at 3:31 am

    You need some sort of identifier for the root comments so that you can sort on them. How about:

    WITH RECURSIVE cte (id, text, date, path, parent_id, depth)  AS (
    SELECT  id,
        text,
        date,
        array[id] AS path,
        parent_id,
        1 AS depth,
        id as root
    FROM    comment
    WHERE parent_id IS NULL
    UNION ALL
    SELECT  comment.id,
        comment.text,
        comment.date,
        cte.path || comment.id,
        comment.parent_id,
        cte.depth + 1 AS depth,
        root
    FROM    comment
    JOIN cte ON comment.parent_id = cte.id
    )
    SELECT id, text, date, path, depth FROM cte
    ORDER BY root desc, path;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an algorithm to layout some hierarchical data but have never
Just need some help taking this hierarchical array... Array ( [root] => Array ([attr]
I have a need to create a diagram viewer. There is hierarchical tree that
I have some hierarchical categories in SQL Server, defined as such: CREATE TABLE [dbo].[CategoryDesc]
I have some hierarchical data that I need display as a table, can I
I have a standard hierarchical table ID/PID and I need to find (boolean) if
I have a highly structured hierarchical directory containing multiple files that need to be
I need to display a tree bound to this kind of hierarchical objects: public
I have a deep hierarchical model that looks like this: Accounts -> Venues ->Events
I have a hierarchical structure like this: class Node { Node Parent; string Name;

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.