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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:52:28+00:00 2026-06-14T11:52:28+00:00

I am trying to use group by in a query in postgres. I cannot

  • 0

I am trying to use group by in a query in postgres. I cannot get it to work as I would like in order to group results as I need.

This is an extension to another stack question on recursion queries I just had answered. But now I need to be able to group the results on the root_id column of the final query. Here is the query before:

select cl.parent_comment_id, 
     cl.article_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
order by cl.root_id, cl.article_comment_id, cl.comment_depth;

Here is what I would like to do so that any records with the same parent_comment_id are kept together.

select cl.parent_comment_id, 
     cl.article_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
group by cl.parent_comment_id
order by cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;

There can be many records with the same parent_comment_id returned, but distinct records for any given article_comment_id. i.e. each comment is unique (id, comment, title, etc.) but each parent comment can have many children. This has already been retrieved by the recursive query, now I am just trying to group them correctly.

Edit:


Take a look at http://www.sqlfiddle.com/#!12/77771/2 .What I would like is for article_comment_id=6 to follow immediately below article_comment_id=3 since id=3 is the parent. Then article_comment_id=4.

However I am thinking this needs to be done procedurally.

So I think this is a ‘never mind’ type of question unless someone knows how (which is why I am leaving it up). But I think I am going to try to solve this part procedurally.

  • 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-14T11:52:29+00:00Added an answer on June 14, 2026 at 11:52 am

    For your recursive query, you can create a hierarchical path using this trick with 0-padded strings: SQL Fiddle

    with recursive comment_list(article_comment_id, parent_comment_id, comment, article_id, comment_depth, comment_path) AS (
        select c.article_comment_id, 
               c.parent_comment_id, 
               c.comment, 
               c.article_id, 
               c.comment_depth,
               substr(CAST(1000000000+c.article_comment_id as varchar(1000)),2)
        from test_comment c
        where article_id = 100
          and parent_comment_id = 0
    
      union all
    
        select c.article_comment_id, 
               c.parent_comment_id, 
               c.comment, 
               c.article_id, 
               c.comment_depth,
               cl.comment_path || substr(CAST(1000000000+c.article_comment_id as varchar(1000)),2)
        from test_comment c
           join comment_list cl on c.parent_comment_id = cl.article_comment_id
    )
    select cl.article_comment_id,
         cl.comment_path, 
         cl.parent_comment_id,
         cl.comment, 
         cl.article_id,
         cl.comment_depth
    from comment_list cl
    order by cl.comment_path, cl.article_comment_id, cl.comment_depth;
    

    Drop the GROUP BY. You want to “group” them for display, which is really “ORDER BY”

    select cl.parent_comment_id, 
         cl.article_comment_id,
         cl.comment, 
         cl.article_id,
         cl.comment_depth
    from comment_list cl
    order by cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;
    

    You may or may not still need the cl.root_id in the order by, so it could be

    order by cl.root_id, cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am need paint my image. I'm trying use JQuery in here this link:
I keep getting this statement Invalid use of group function for this query mysql_query(UPDATE
I'm trying the query below and MySQL gave me this error: Invalid use of
Good afternoon, I am trying to use CASE like in this question to transpose
I'm trying to use custom query in Cake then paginate the results with the
I've got this code I'm trying to use to export data from Excel to
Greetings, I'm trying to use the 'group' parameter for a find using CakePHP. The
I have an error in my query sub query. I been trying to work
I'm trying to use group by with rollup clause within sql server 2005 but
In the query below, I am trying to use the first table in a

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.