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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:36:46+00:00 2026-05-22T17:36:46+00:00

Suppose there is a MSSQL table, UserPost , that represents something user has posted,

  • 0

Suppose there is a MSSQL table, UserPost, that represents something user has posted, with these fields:

ID | dateAdded | parentPostID | postBody

A user in the system could create a Request, receive a Response, and then other users could Comment on the Response. Ie, Request <=1:many=> Reponse <=1:many=> Comment (think StackOverlow’s Question > Answer > Comment model-like).

All user posts (Request, Response and Comment) are represented by UserPost rows, where Request has parentPostID = null;; Responses’ parentPostID is the Request’s ID, and Comment’s parentPostID is the ID of the Response.

I need to output everything in a simple fashion:

Request 1
- Response A
-- Comment (i)
-- Comment (ii)
- Response B
-- Comment (i)
Request 2
...

Question: which SQL statement returns the needed information in the most usable way?

I’m struggling to write a three-way join between (UserPosts) as Requests [join] (UserPosts) as Responses [join] (UsersPosts) as Comments but am not sure this is the easiest way.

Bonus: is it possible to do this using C# Linq?

  • 1 1 Answer
  • 1 View
  • 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-22T17:36:47+00:00Added an answer on May 22, 2026 at 5:36 pm

    Can’t think of a way to do this in LINQ. I’ve removed unused columns. Luckily this is a bounded hierarchy. I’m using the new hierarchyid data type, which has the desired sort order:

    create table UserPosts (
        ID int not null,
        ParentID int null
    )
    go
    insert into UserPosts (ID,ParentID)
    select 1,null union all
    select 2,null union all
    select 3,1 union all
    select 4,2 union all
    select 5,3 union all
    select 6,1 union all
    select 7,6
    go
    select
        *
    from
        UserPosts up
            left join
        UserPosts up_1st
            on
                up.ParentID = up_1st.ID
            left join
        UserPosts up_2nd
            on
                up_1st.ParentID = up_2nd.ID
    order by
        CONVERT(hierarchyid,
        COALESCE('/' + CONVERT(varchar(10),up_2nd.ID),'') +
        COALESCE('/' + CONVERT(varchar(10),up_1st.ID),'') +
        '/' + CONVERT(varchar(10),up.ID) + '/'
        )
    

    HierarchyIDs (as strings) look like /GrandParent/Parent/Child/ – so we construct values that look like this. Obviously, if we don’t have a grandparent (up_2nd.ID is null, since we can’t achieve 2 left joins as described), then we just want to construct /Parent/Child/ – this is what the 1st COALESCE is helping us achieve. Similarly, if we can’t find any parents (both up_1st.ID and up_2nd.ID are null), then both of the COALESCEs just turn into empty strings, and we end up construcing /ID/.


    You can add:

    CASE
        WHEN up_2nd.ID is not null then 'Comment'
        WHEN up_1st.ID is not null then 'Response'
        ELSE 'Request'
    END as Level
    

    to your select list, if you want to track what level the item is (or use numerics instead, if desired)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose there is a table A which has a column AccessRights which is multivalued(
Suppose there is an image_url column in database. I want the user to choose
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose Table X has a 100 tuples. Will the following approach to scanning X
Suppose I have a MySQL table that keeps the page visit count and I
Suppose there are two tables. Table X-- Columns: id x_value Table Y-- Columns: id
Ok, let's suppose we have members table. There is a field called, let's say,
I've 2 mysql select statements, suppose these are my queries & their result :
Suppose there are two scripts Requester.php and Provider.php, and Requester requires processing from Provider

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.