views:

55

answers:

2

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
A: 

Take a look at Cursors Here.

I personally do not like them when I can avoid them, but it would give you a way to grab one Parent at a time, and then all that parent's children.

AllenG
As you said Curser will be my last option, I would like to try above solution, looks like it's going to work.Thanks for your reply.
+1  A: 

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)
KM
My Id and parentID id both are uniqueidentifier fields, I get following error.Msg 291, Level 16, State 1, Line 5CAST or CONVERT: invalid attributes specified for type 'uniqueidentifier'
@user228777, see edit
KM
+1. Far better than a cursor. But, then, I think cursors were invented by Satan...
AllenG
Thank you very much KM. It worked like a charm. Thanks again.