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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:11:42+00:00 2026-06-01T13:11:42+00:00

I am trying to come up with a query that will return comments following

  • 0

I am trying to come up with a query that will return comments following their replies and its replies.

Something like

comment 1
reply 1.1
reply 1.1.1
reply 1.2
comment 2
comment 3
comment 3.1 

etc

I have this so far

SELECT [CommentID]
  ,[ParentID]
  ,[Message]
  , ROW_NUMBER() over(partition by ParentID order by CommentID ) as rn
  ,[CreatedBy]
  ,[CreatedDate]
  FROM [DBNAME].[dbo].[Commenttable] 
  GROUP BY [CommentID],[ParentID],[CreatedDate],[Message],[CreatedBy]

but what I get is

comment 1
comment 2
comment 3
reply 1.1
reply 1.2
reply 3.1
reply 1.1.1

Basic Structure is just a table with Comment ID, Parent ID, and the message.
The comments and replies are just to help explain what I am trying to achieve

  • 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-01T13:11:43+00:00Added an answer on June 1, 2026 at 1:11 pm

    Give this a try:

    declare @CommentTable as Table ( CommentId Int Identity, ParentId Int Null, Message VarChar(16) )
    insert into @CommentTable ( ParentId, Message ) values
      ( null, '1' ),
      ( null, '2' ), ( 1, '1.1' ),
      ( null, '3' ), ( 4, '3.1' ), ( 3, '1.1.1' ), ( 1, '1.2' )
    select * from @CommentTable
    
    ; with Cindy as (
      -- Start with the base comments.
      select CommentId, ParentId, Message, Row_Number() over ( order by CommentId ) as Number,
        Cast( Row_Number() over ( order by CommentId ) as VarChar(1000) ) as Path,
        Cast( Right( '0000' + Cast( Row_Number() over ( order by CommentId ) as VarChar(4) ), 5 ) as VarChar(1000) ) as OrderPath
        from @CommentTable
        where ParentId is NULL
      union all
      -- Add replies on layer at a time.
      select CT.CommentId, CT.ParentId, CT.Message, Row_Number() over ( order by CT.CommentId ),
        Cast( C.Path + '.' + Cast( Row_Number() over ( order by CT.CommentId ) as VarChar(4) ) as VarChar(1000) ),
        Cast( C.OrderPath + Right( '0000' + Cast( Row_Number() over ( order by CT.CommentId ) as VarChar(4) ), 5 ) as VarChar(1000) )
        from @CommentTable as CT inner join
          Cindy as C on C.CommentId = CT.ParentId
      )
      select *
        from Cindy
        order by OrderPath
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to come up with a MySQL query that will update points ...
I'm trying to come up with a Java regex that will match a filename
I am trying to come up with an insert sql stament that will insert
I'm trying to write a function that will remove a query argument from a
I'm trying to write a query that will display the minimum value (lowest score)
I'm trying to construct a query that will map two columns, one, a date
I'm trying to come up with a way to query dates in Lucene. Basically
I'm trying to come up with a way to query the values in two
I'm trying to come up with a way to query my blog database to
I am trying to come up with such a solution that the user is

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.