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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:48:02+00:00 2026-05-27T15:48:02+00:00

i have the following table data in ms sql server table name category and

  • 0

i have the following table data in ms sql server table name category

table data and schema

and the problem is that i want to get the data of grand parent,parent,siblings,child and sub child and self.

i hope you get my point , if need any more clarification from my requirement i can edit my question just put a comment below.

as far about my try i searched stackoverflow and i found a lot of examples of getting full hierarchical data using parent but don’t find anything which is related to pass the child and get parent, sub child and self.

I am also open for the solution which provide me solution using linq because then i can take complete data in Category and can use linq their on .cs page.

Edit :
if i pass the 7 which is heritage CategoryId then the query should returns the following rows

desired result in case category id 7 pass

  • 1 1 Answer
  • 2 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-27T15:48:03+00:00Added an answer on May 27, 2026 at 3:48 pm

    The answer is to use a recusive “Common Table Expression”, or CTE. This allows you to build the structure of your hierarchy. Below is an example, modified to match your structure, based on this page: http://msdn.microsoft.com/en-us/library/ms186243.aspx

    WITH CategoryStructured (ParentCategoryID, CategoryID, Description, Status, Level) 
    AS 
    ( 
    -- Anchor member definition 
    SELECT c.ParentCategoryID, c.CategoryID, c.Description, c.Status, 
    0 AS Level 
    FROM Category AS c 
    WHERE c.ParentCategoryID=0 
    UNION ALL 
    -- Recursive member definition 
    SELECT c.ParentCategoryID, c.CategoryID, c.Description, c.Status, 
    Level + 1 
    FROM Category AS c 
    
    INNER JOIN CategoryStructured AS c_parent 
    ON c.ParentCategoryID = c_parent.CategoryID 
    ) 
    -- Statement that executes the CTE 
    SELECT distinct cs.ParentCategoryID, cs.CategoryID, cs.Description, cs.Status, cs.Level 
    FROM 
    CategoryStructured cs, 
    
    
    (SELECT level,ParentCategoryID,CategoryID from CategoryStructured WHERE (categoryID = 4) OR (level = 1 AND parentCategoryID = 4)) as thisCategory 
    
    
    WHERE cs.level BETWEEN thisCategory.level - 1 AND thisCategory.level+1 
    AND ((thisCategory.level != 0 AND cs.ParentCategoryID = thisCategory.ParentCategoryID) 
    OR cs.categoryID = thisCategory.ParentCategoryID 
    OR cs.ParentCategoryID = thisCategory.CategoryID 
    OR cs.CategoryID = thisCategory.CategoryID)
    

    Updated to reflect your updated question.

    edit I know you were able to get the above basically working for you with the added distinct, but I thought of a better way to handle this after I left chat:

    WITH CategoryStructured (ParentCategoryID, CategoryID, Description, Status, Level)
    AS
    (
    -- Anchor member definition
        SELECT c.ParentCategoryID, c.CategoryID, c.Description, c.Status, 
            0 AS Level
        FROM Categories AS c
        WHERE 
         (c.ParentCategoryID IS NULL AND c.categoryID = 7) -- when 7 is a top level category, then it is the root level
         OR (c.categoryID = (SELECT c2.parentCategoryID FROM Categories c2 WHERE c2.categoryID = 7)) -- when 7 is some non-top level category, then 7's parent is the root
        UNION ALL
    -- Recursive member definition
        SELECT c.ParentCategoryID, c.CategoryID, c.Description, c.Status, 
            Level + 1
        FROM Categories AS c
    
        INNER JOIN CategoryStructured AS c_parent
            ON c.ParentCategoryID = c_parent.CategoryID
    )
    -- Statement that executes the CTE
    SELECT cs.ParentCategoryID, cs.CategoryID, cs.Description, cs.Status, cs.Level
    FROM 
      CategoryStructured cs
    WHERE cs.level < 3
    ORDER BY cs.level
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and data in SQL Server 2005: create table LogEntries
In SQL Server 2008, I have a table with following columns and data Date
I have a sql table which have the following data, Id City Country ---
Suppose we have the following table data: ID parent stage submitted 1 1 1
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have table that I insert data with following query (from c# code): INSERT
I have a table emp with following structure and data: name dept salary -----
I have a Access Data Project that connects to an SQL server database. Recently
I have the following two table in SQL server 2005 DB 2- UserInfomration --->
I have a table that stores data from the county appraisal district. I want

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.