SQL Recursive CTE - Keep reference to a parent

user3362714

I have a table with the following structure

userId      userName         managerId
----------- ---------------- -----------
1           John             NULL
2           Charles          1
3           Nicolas          NULL
4           Neil             3

And I have another table which has the following

userId      shareId         
----------- -----------
1           1001             
3           1002               

So I do the following query to get my recursive CTE:

WITH UserCTE AS (
  SELECT userId, userName, managerId,0 AS steps
  FROM dbo.Users
  WHERE userId = 7

  UNION ALL

  SELECT mgr.userId, mgr.userName, mgr.managerId, usr.steps +1 AS steps
  FROM UserCTE AS usr
    INNER JOIN dbo.Users AS mgr
      ON usr.managerId = mgr.userId
)
SELECT * FROM UserCTE AS u;         

Which produces the following result

userId      userName         managerId   steps       
----------- ---------------- ----------- ----------- 
1           John             NULL        0           
2           Charles          1           1           
3           Nicolas          NULL        0           
4           Neil             3           1      

OK so what I want to know is the shareId for the users that have it, and also for the users that belong to them.

Expected result :

userId      userName         managerId   steps       shareId
----------- ---------------- ----------- ----------- ----------
1           John             NULL        0           1001
2           Charles          1           1           1001
3           Nicolas          NULL        0           1002
4           Neil             3           1           1002

Is there any way to achieve it?

Thanks

Pரதீப்

Join Share table with the Users table in anchor query of Recursive CTE. Try this.

;WITH UserCTE
     AS (SELECT c.userId,
                userName,
                managerId,
                0 AS steps,
                shareId
         FROM   dbo.Users c
                LEFT JOIN share_table s
                  ON c.userId = s.userId
         WHERE  managerId IS NULL
         UNION ALL
         SELECT mgr.userId,
                mgr.userName,
                mgr.managerId,
                usr.steps + 1 AS steps,
                usr.shareId
         FROM   UserCTE AS usr
                INNER JOIN dbo.Users AS mgr
                        ON usr.userId = mgr.managerId)
SELECT *
FROM   UserCTE AS u
ORDER  BY userId; 

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

T-SQL Recursive CTE with Multiple Parent Items

From Dev

Recursive CTE in SQL

From Dev

SQL group with Recursive CTE

From Dev

SQL complicated recursive CTE

From Dev

SQL: Optimizing Recursive CTE

From Dev

Recursive cte sql with for hierarchy level

From Dev

Recursive SQL CTE query in Pandas?

From Dev

SQL Recursive CTE Linked Chains

From Dev

Recursive CTE query in SQL Server

From Dev

Recursive CTE of Dates Netezza SQL

From Dev

Find Top level Parent and With Recursive CTE

From Dev

SQL - Use a reference of a CTE to another CTE

From Dev

SQL - Use a reference of a CTE to another CTE

From Dev

Creating permutation via recursive CTE in SQL server?

From Java

Slow performance on Recursive CTE in T-SQL

From Dev

How does this recursive SQL CTE work exactly?

From Dev

SQL Server Recursive CTE - Why this behavior?

From Dev

Convert a Recursive CTE in SQL Server to netezza

From Dev

T-SQL Recursive Function - CTE with ParentID

From Dev

sql recursive cte in view not work fine

From Dev

SQL Server Recursive CTE - Why this behavior?

From Dev

Recursive CTE with 2 tables not working in Oracle / SQL

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

Reference column in WHERE EXISTS in a SQL Server CTE?

From Dev

SQL Server Parent/Child CTE Ordering issue

From Dev

SQL Server - CTE Recursive SUM Value From Different Table

From Dev

SQL Server - CTE Recursive, Looping in Child's Data?