SQL Server: How to get top parent id and last child id from parent child hierarchy

T.Zacks 3,996 Reputation points
2022-01-03T19:18:35.533+00:00

Suppose i have a table which has column like ID, ParentID, Name

data store like
+─────+───────────+──────────────────+
| ID | ParentID | Name |
+─────+───────────+──────────────────+
| 1 | 0 | Model |
| 2 | 1 | Consensus Model |
| 3 | 1 | Segment Details |
| 4 | 3 | Key Financials |
+─────+───────────+──────────────────+

1) How to know what is top parent of Key Financials which is Consensus Mode.
2) how to know what is the last child of Consensus Mode in hierarchy?

what sql i need to issue to get result for my 2 questions.

Thanks

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. LiHong-MSFT 10,056 Reputation points
    2022-01-04T06:52:28.187+00:00

    Hi,@T.Zacks
    It is a good way to use recursive CTE to get the parent child hierarchy.
    Please check this:

    CREATE TABLE #table(ID INT,ParentID INT,Name VARCHAR(50))  
    INSERT INTO #table VALUES(1, 0,' Model'),(2, 1,'Consensus Model '),(3, 1,'Segment Details'),(4, 3,'Key Financials')  
      
    --Query from parent to child  
    ;WITH SubsCTE AS  
    	(  
    	 --Anchor Member (Return the row where the employee is entered)  
    	 select  ID,ParentID,Name,0 as lvl  
    	 from #table   
    	 where ID = 1  
    	 UNION ALL  
    	 --Recursive Member (Return recursive members of subordinates)  
    	 select C.ID,C.ParentID,C.Name,P.lvl + 1  
    	 from SubsCTE P JOIN #table C   
    	      ON C.ParentID = P.ID  
    	)  
    SELECT * FROM SubsCTE  
      
    --Query from child to parent   
    ;WITH MgrsCTE AS  
    	(  
    	 select ID,ParentID,Name,0 as lvl  
    	 from #table   
    	 where ID = 3  
    	 UNION ALL  
    	 select P.ID,P.ParentID,P.Name,C.lvl + 1  
    	 from MgrsCTE C JOIN #table P   
    	      ON C.ParentID = P.ID  
    	)  
    SELECT * FROM MgrsCTE  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2022-01-03T23:02:14.227+00:00

    1) How to know what is top parent of Key Financials which is Consensus Model.

    Is it? why does it have 3 in ParentID? That is Segment details, which appears to be a sibling to to Consensus model.

    If you have not worked recursive queries before, I found this tutorial for you: https://www.mssqltips.com/sqlservertip/1520/recursive-queries-using-common-table-expressions-cte-in-sql-server/

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.