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.