Find Top level Parent and With Recursive CTE

InTheWorldOfCodingApplications

I have got two tables as following

 Table Person

  Id   Name
  1    A
  2    B
  3    C
  4    D
  5    E

Table RelationHierarchy

ParentId   CHildId
    2         1
    3         2
    4         3

This will form a tree like structure

      D
      |
      C
      |
      B
      |
      A

I am using following CTE to find top level parent

  DECLARE @childID INT 
  SET @childID  = 1 --chield to search

  ;WITH RCTE AS
  (
    SELECT *, 1 AS Lvl FROM RelationHierarchy 
    WHERE ChildID = @childID

    UNION ALL

    SELECT rh.*, Lvl+1 AS Lvl FROM dbo.RelationHierarchy rh
    INNER JOIN RCTE rc ON rh.CHildId = rc.ParentId
    )
   SELECT TOP 1 id, Name
   FROM RCTE r
   inner JOIN dbo.Person p ON p.id = r.ParentId
   ORDER BY lvl DESC

Now the problem is , incase the Person is himself a toplevel then his Id should be returned instead of no records. Do i need a temp table or table variable to check count of CTE and then return the @childID if Count is Zero?

Gordon Linoff

I think this solves your problem. The idea is to force the start on the child rather than doing a lookup initially:

WITH RCTE AS
  (
    SELECT @childID as parentId, NULL as childid, 1 AS Lvl

    UNION ALL

    SELECT rh.*, Lvl+1 AS Lvl FROM dbo.RelationHierarchy rh
    INNER JOIN RCTE rc ON rh.CHildId = rc.ParentId
    )
   SELECT TOP 1 id, Name
   FROM RCTE r
   inner JOIN dbo.Person p ON p.id = r.ParentId
   ORDER BY lvl DESC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RECURSIVE CTE SQL - Find next available Parent

From Dev

Find multiple top level parents in CTE

From Dev

Find top-level parent in List with LINQ

From Dev

SQL Server 2012 CTE Find Root or Top Parent of Hierarchical Data

From Dev

Recursive cte sql with for hierarchy level

From Dev

The most efficient way to find the top level parent in SQL Server?

From Dev

Find the top-level parent group each record belongs to

From Dev

To find infinite recursive loop in CTE

From Dev

SQL Recursive CTE - Keep reference to a parent

From Dev

Recursive CTE to find all ancestors OF ALL ITEMS

From Dev

find in jQuery object top level

From Dev

T-SQL Recursive CTE with Multiple Parent Items

From Dev

The Recursive CTE to query all ancestors of a Parent-Child table is slow

From Dev

Recursive query with CTE - Concatenate all fields of child elements and add to parent

From Dev

Recursive CTE-Find all Employees Below Manager

From Dev

T-SQL Recursive CTE to find Highest ParentID

From Dev

Recursive CTE-Find all Employees Below Manager

From Dev

Reverse recursive in array to find parent id

From Dev

Return to top-level call of a recursive function in Lisp

From Dev

.find() top level <p> tags within div

From Dev

Find top level functions in R file

From Dev

Get n level parent with find command

From Dev

Find a file and delete the parent level dir

From Dev

CTE recursive query

From Dev

CTE Recursive Queries

From Dev

Issue with recursive CTE in PostgreSQL

From Dev

Understanding steps of recursive CTE

From Dev

Recursive CTE with alternating tables

From Dev

Recursive CTE Bill of Materials