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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:12:03+00:00 2026-05-24T14:12:03+00:00

I have a single table with a self reference InReplyTo with some data like

  • 0

I have a single table with a self reference InReplyTo with some data like this:

PostID InReplyTo Depth
------ --------- -----
1      null      0
2      1         1
3      1         1
4      2         2
5      3         2
6      4         3
7      1         1
8      5         3
9      2         2

I want to write a query that will return this data in it’s threaded form so that the post with ID=2 and all it’s descendants will output before PostID=3 and so on for unlimited depth

PostID InReplyTo Depth
------ --------- -----
1      null      0
2      1         1
4      2         2
6      4         3
9      2         2
3      1         1
5      3         2
8      5         3
7      1         1

Is there a simple way to achieve this? I am able to modify the DB structure at this stage so would the new hierarchy datatype be the easiest way to go? Or perhaps a recursive CTE?

  • 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-05-24T14:12:04+00:00Added an answer on May 24, 2026 at 2:12 pm
    -- Test table
    declare @T table (PostID int, InReplyTo int, Depth int)
    
    insert into @T values  (1,  null, 0), (2,  1,  1), (3,  1,  1), (4,  2,  2), 
    (5,  3,  2), (6,  4,  3), (7,  1,  1), (8,  5,  3),(9,  2,  2)
    
    -- The post to get the hierarchy from
    declare @PostID int = 1
    
    -- Recursive cte that builds a string to use in order by
    ;with cte as
    (
      select T.PostID,
             T.InReplyTo,
             T.Depth,
             right('0000000000'+cast(T.PostID as varchar(max)), 10)+'/' as Sort
      from @T as T
      where T.PostID = @PostID
      union all
      select T.PostID,
             T.InReplyTo,
             T.Depth,
             C.Sort+right('0000000000'+cast(T.PostID as varchar(max)), 10)+'/'
      from @T as T
        inner join cte as C
          on T.InReplyTo = C.PostID
    )
    select PostID,
           InReplyTo,
           Depth,
           Sort
    from cte
    order by Sort
    

    Result:

    PostID      InReplyTo   Depth       Sort
    ----------- ----------- ----------- --------------------------------------------
    1           NULL        0           0000000001/
    2           1           1           0000000001/0000000002/
    4           2           2           0000000001/0000000002/0000000004/
    6           4           3           0000000001/0000000002/0000000004/0000000006/
    9           2           2           0000000001/0000000002/0000000009/
    3           1           1           0000000001/0000000003/
    5           3           2           0000000001/0000000003/0000000005/
    8           5           3           0000000001/0000000003/0000000005/0000000008/
    7           1           1           0000000001/0000000007/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dbml with single table users i want add partial class for User
Let's say you have a database with a single table like... --------------------------------------------- | Name
I have a table that displays data from a single array and organizes the
I have a table (this cannot be changed) like the following: POST_ID | PARENT_ID
I'm working on a project that will have a single table holding lots and
I have a form that searches all rows in a single table (TServices) that
I have a huge access mdb file which contains a single table with 20-30
I have a single entry output from a paradox table which is imported into
I have a database containing a single huge table. At the moment a query
I have a table where on a single record update, a trigger adds 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.