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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:02:00+00:00 2026-05-15T08:02:00+00:00

I am trying to get recursive data. Following code returns all parents on the

  • 0

I am trying to get recursive data.
Following code returns all parents on the top and then the children.
I would like to get data Parent 1 – his children then parent 2 – his children then parent3 – his children.
How do I do this?

USE  Subscriber
GO
WITH Parent (ParentId, Id, Name,subscriberID)
AS
(
-- Anchor member definition
    SELECT A.ParentId,A.id, A.name,A.SubscriberId

    FROM Subscriber.Budget.SubscriberCategory AS A   
    WHERE ParentId IS NULL
    UNION ALL
-- Recursive member definition
    SELECT B.ParentId, B.id, B.name,B.SubscriberId

    FROM Subscriber.Budget.SubscriberCategory AS B 

    INNER JOIN Parent AS P


ON B.ParentId = P.Id
)

-- Statement that executes the CTE
SELECT parentId, id, name
FROM Parent
where subscriberID = '1C18093B-5031-42E4-9251-CEF69114365F'

GO
  • 1 1 Answer
  • 0 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-15T08:02:00+00:00Added an answer on May 15, 2026 at 8:02 am

    here is a generic solution that the OP can map to their tables/columns:

    set up data

    DECLARE @Staff table (UserID char(4), UserName varchar(10), ManagerID char(4))
    INSERT @Staff VALUES ('ABC1','Jerome', NULL )
    INSERT @Staff VALUES ('ABC2','Joe'   ,'ABC1')
    INSERT @Staff VALUES ('ABC3','Paul'  ,'ABC2')
    INSERT @Staff VALUES ('ABC4','Jack'  ,'ABC3')
    INSERT @Staff VALUES ('ABC5','Daniel','ABC3')
    INSERT @Staff VALUES ('ABC6','David' ,'ABC2')
    INSERT @Staff VALUES ('ABC7','Ian'   ,'ABC6')
    INSERT @Staff VALUES ('ABC8','Helen' ,'ABC6')
    INSERT @Staff VALUES ('ABC9','Sam'   , NULL)
    INSERT @Staff VALUES ('ABD1','Ron'   ,'ABC9')
    INSERT @Staff VALUES ('ABD2','Bill'  ,'ABC9')
    INSERT @Staff VALUES ('ABD3','Fred'  ,'ABD1')
    
    DECLARE @RootUserID  char(4)
    SET @RootUserID='ABC2'
    

    get complete tree

    ;WITH StaffTree AS
    (
        SELECT 
            UserID, UserName, ManagerID, CONVERT(char(4),NULL) AS ManagerUserID, 1 AS LevelOf
                ,CONVERT(varchar(max),UserID) AS ChainOfCommand
            FROM @Staff
            WHERE ManagerID IS NULL
        UNION ALL
            SELECT 
                s.UserID, s.UserName, s.ManagerID, t.UserID, t.LevelOf+1
                    ,ISNULL(t.ChainOfCommand,'')+'|'+s.ManagerID
            FROM StaffTree         t
                INNER JOIN @Staff  s ON t.UserID=s.ManagerID
    
    )
    SELECT * FROM StaffTree ORDER BY ChainOfCommand,UserID
    

    get tree of given user

    ;WITH StaffTree AS
    (
        SELECT 
            UserID, UserName, ManagerID, CONVERT(char(4),NULL) AS ManagerUserID, 1 AS LevelOf
                ,CONVERT(varchar(max),UserID) AS ChainOfCommand
            FROM @Staff
            WHERE UserID=@RootUserID
        UNION ALL
            SELECT 
                s.UserID, s.UserName, s.ManagerID, t.UserID, t.LevelOf+1
                    ,ISNULL(t.ChainOfCommand,'')+'|'+s.ManagerID
            FROM StaffTree         t
                INNER JOIN @Staff  s ON t.UserID=s.ManagerID
            WHERE s.ManagerID=@RootUserID
    
    )
    SELECT * FROM StaffTree ORDER BY ChainOfCommand,UserID
    

    OUTPUT:

    UserID UserName   ManagerID ManagerUserID LevelOf     ChainOfCommand
    ------ ---------- --------- ------------- ----------- ------------------------
    ABC1   Jerome     NULL      NULL          1           ABC1
    ABC2   Joe        ABC1      ABC1          2           ABC1|ABC1
    ABC3   Paul       ABC2      ABC2          3           ABC1|ABC1|ABC2
    ABC6   David      ABC2      ABC2          3           ABC1|ABC1|ABC2
    ABC4   Jack       ABC3      ABC3          4           ABC1|ABC1|ABC2|ABC3
    ABC5   Daniel     ABC3      ABC3          4           ABC1|ABC1|ABC2|ABC3
    ABC7   Ian        ABC6      ABC6          4           ABC1|ABC1|ABC2|ABC6
    ABC8   Helen      ABC6      ABC6          4           ABC1|ABC1|ABC2|ABC6
    ABC9   Sam        NULL      NULL          1           ABC9
    ABD1   Ron        ABC9      ABC9          2           ABC9|ABC9
    ABD2   Bill       ABC9      ABC9          2           ABC9|ABC9
    ABD3   Fred       ABD1      ABD1          3           ABC9|ABC9|ABD1
    
    (12 row(s) affected)
    
    UserID UserName   ManagerID ManagerUserID LevelOf     ChainOfCommand
    ------ ---------- --------- ------------- ----------- ------------------------
    ABC2   Joe        ABC1      NULL          1           ABC2
    ABC3   Paul       ABC2      ABC2          2           ABC2|ABC2
    ABC6   David      ABC2      ABC2          2           ABC2|ABC2
    
    (3 row(s) affected)
    

    EDIT basic edit of above code to use uniqueidentifier IDs:

    DECLARE @Staff table (UserID uniqueidentifier, UserName varchar(10), ManagerID uniqueidentifier)
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B0D','Jerome', NULL )
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B02','Joe'   ,'6A3BEB4C-D116-481E-B98D-4779246C4B0D')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B03','Paul'  ,'6A3BEB4C-D116-481E-B98D-4779246C4B02')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B04','Jack'  ,'6A3BEB4C-D116-481E-B98D-4779246C4B03')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B05','Daniel','6A3BEB4C-D116-481E-B98D-4779246C4B03')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B06','David' ,'6A3BEB4C-D116-481E-B98D-4779246C4B02')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B07','Ian'   ,'6A3BEB4C-D116-481E-B98D-4779246C4B06')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B08','Helen' ,'6A3BEB4C-D116-481E-B98D-4779246C4B06')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B09','Sam'   , NULL)
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B11','Ron'   ,'6A3BEB4C-D116-481E-B98D-4779246C4B09')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B12','Bill'  ,'6A3BEB4C-D116-481E-B98D-4779246C4B09')
    INSERT @Staff VALUES ('6A3BEB4C-D116-481E-B98D-4779246C4B13','Fred'  ,'6A3BEB4C-D116-481E-B98D-4779246C4B11')
    
    
    DECLARE @RootUserID  char(4)
    SET @RootUserID='6A3BEB4C-D116-481E-B98D-4779246C4B02'
    
    ;WITH StaffTree AS
    (
        SELECT 
            UserID, UserName, ManagerID, CONVERT(uniqueidentifier,NULL) AS ManagerUserID, 1 AS LevelOf
                ,CONVERT(varchar(max),UserID) AS ChainOfCommand
            FROM @Staff
            WHERE ManagerID IS NULL
        UNION ALL
            SELECT 
                s.UserID, s.UserName, s.ManagerID, t.UserID, t.LevelOf+1
                    ,ISNULL(t.ChainOfCommand,'')+'|'+CONVERT(varchar(max),s.ManagerID)
            FROM StaffTree         t
                INNER JOIN @Staff  s ON t.UserID=s.ManagerID
    
    )
    SELECT * FROM StaffTree ORDER BY ChainOfCommand,UserID
    

    OUTPUT:

    UserID                               UserName   ManagerID                            ManagerUserID                        LevelOf     ChainOfCommand
    ------------------------------------ ---------- ------------------------------------ ------------------------------------ ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------
    6A3BEB4C-D116-481E-B98D-4779246C4B09 Sam        NULL                                 NULL                                 1           6A3BEB4C-D116-481E-B98D-4779246C4B09
    6A3BEB4C-D116-481E-B98D-4779246C4B11 Ron        6A3BEB4C-D116-481E-B98D-4779246C4B09 6A3BEB4C-D116-481E-B98D-4779246C4B09 2           6A3BEB4C-D116-481E-B98D-4779246C4B09|6A3BEB4C-D116-481E-B98D-4779246C4B09
    6A3BEB4C-D116-481E-B98D-4779246C4B12 Bill       6A3BEB4C-D116-481E-B98D-4779246C4B09 6A3BEB4C-D116-481E-B98D-4779246C4B09 2           6A3BEB4C-D116-481E-B98D-4779246C4B09|6A3BEB4C-D116-481E-B98D-4779246C4B09
    6A3BEB4C-D116-481E-B98D-4779246C4B13 Fred       6A3BEB4C-D116-481E-B98D-4779246C4B11 6A3BEB4C-D116-481E-B98D-4779246C4B11 3           6A3BEB4C-D116-481E-B98D-4779246C4B09|6A3BEB4C-D116-481E-B98D-4779246C4B09|6A3BEB4C-D116-481E-B98D-4779246C4B11
    6A3BEB4C-D116-481E-B98D-4779246C4B0D Jerome     NULL                                 NULL                                 1           6A3BEB4C-D116-481E-B98D-4779246C4B0D
    6A3BEB4C-D116-481E-B98D-4779246C4B02 Joe        6A3BEB4C-D116-481E-B98D-4779246C4B0D 6A3BEB4C-D116-481E-B98D-4779246C4B0D 2           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D
    6A3BEB4C-D116-481E-B98D-4779246C4B03 Paul       6A3BEB4C-D116-481E-B98D-4779246C4B02 6A3BEB4C-D116-481E-B98D-4779246C4B02 3           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02
    6A3BEB4C-D116-481E-B98D-4779246C4B06 David      6A3BEB4C-D116-481E-B98D-4779246C4B02 6A3BEB4C-D116-481E-B98D-4779246C4B02 3           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02
    6A3BEB4C-D116-481E-B98D-4779246C4B04 Jack       6A3BEB4C-D116-481E-B98D-4779246C4B03 6A3BEB4C-D116-481E-B98D-4779246C4B03 4           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02|6A3BEB4C-D116-481E-B98D-4779246C4B03
    6A3BEB4C-D116-481E-B98D-4779246C4B05 Daniel     6A3BEB4C-D116-481E-B98D-4779246C4B03 6A3BEB4C-D116-481E-B98D-4779246C4B03 4           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02|6A3BEB4C-D116-481E-B98D-4779246C4B03
    6A3BEB4C-D116-481E-B98D-4779246C4B07 Ian        6A3BEB4C-D116-481E-B98D-4779246C4B06 6A3BEB4C-D116-481E-B98D-4779246C4B06 4           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02|6A3BEB4C-D116-481E-B98D-4779246C4B06
    6A3BEB4C-D116-481E-B98D-4779246C4B08 Helen      6A3BEB4C-D116-481E-B98D-4779246C4B06 6A3BEB4C-D116-481E-B98D-4779246C4B06 4           6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B0D|6A3BEB4C-D116-481E-B98D-4779246C4B02|6A3BEB4C-D116-481E-B98D-4779246C4B06
    
    (12 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 427k
  • Answers 427k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a Keyboard Accessory View. Here is a sample project May 15, 2026 at 12:48 pm
  • Editorial Team
    Editorial Team added an answer First off, that's not how you use DateDiff. This is… May 15, 2026 at 12:48 pm
  • Editorial Team
    Editorial Team added an answer It seems that the datasource is not recognized in the… May 15, 2026 at 12:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.