0

Hi I am trying to get the full hierarchy of my category. Here is my sample table

ID     PARENT_ID       NAME          DEPTH
------------------------------------------
1      NULL            A             1
2      NULL            B             1
3      NULL            C             1
4      1               D             2
5      4               E             3

The cte output should be this

ID     PARENT_ID       NAME
---------------------------
1      NULL            A             
2      NULL            B             
3      NULL            C             
4      1               D             
5      4               E             
5      1               E  

As you can see id:5 is parent of 4 and 1. How do i query the whole tree

3 Answers 3

3
DECLARE @tmp TABLE(ID INT,ParentID INT,NAME VARCHAR(10),DEPTH INT)

INSERT INTO @tmp VALUES
(1      ,NULL            ,'A'             ,1 ),
(2      ,NULL            ,'B'             ,1 ),
(3      ,NULL            ,'C'             ,1 ),
(4      ,1               ,'D'             ,2 ),
(5      ,4               ,'E'            ,3 ),
(6      ,5               ,'F'            ,4 );

select * from @tmp

;WITH cte AS
(
    SELECT   A.ID
            ,A.ParentID
            ,A.NAME
            ,A.DEPTH
    FROM    @tmp A

    UNION ALL

    SELECT   A.ID
            ,B.ParentID
            ,A.NAME
            ,A.DEPTH 
    FROM    cte A 
    INNER JOIN @tmp B on A.ParentID = B.ID 
    WHERE   B.ParentID IS NOT NULL

)

SELECT * FROM cte
2
  • The query will not work if there are more than one row at the same depth, i.e. if there were three more rows at depth 3 and with parent 4
    – Asher
    Dec 15, 2011 at 7:56
  • Hello, I have posted another question about this topic here: stackoverflow.com/a/8517097/184773. Can you check it out for me. I see you are very knowledgable about sql server.
    – Luke101
    Dec 16, 2011 at 1:49
1

In SQL Server 2008 CTE can be use to query recursively.

Example of CTE from MSDN

-- Sample Solution (Untested)

;With TableCTE(Id, Name, ParentId, Depth)
(
   Select ID,Name,ParentId, 1
     FROM MyTable

   Union All

   Select C.Id, C.Name, t.ParentId, c.Depth +1
     FROM @tmp t
    INNER JOIN TableCTE C on t.Id = c.ParentId
    -- Where t.ParentId IS Not Null
)

SELECT Id, Name, ParentId
FROM TableCTE
3
  • I tried this but it did not return that id: 5 is a parent of id:1
    – Luke101
    Dec 13, 2011 at 4:12
  • CTE loops recursively till it returns any rows, so if you want to loop back we have to tweak a query, please see the updated answer. Dec 13, 2011 at 15:31
  • Removed the 'Where t.ParentId IS NOT NULL' clause as it was giving erroneous results i.e. Depth 1 and 2 were all seen as depth 1 and the subsequent depths were 1 less than what they should be Apr 11, 2014 at 6:38
0
;WITH cte AS
(
    SELECT   A.ID
            ,A.ParentID
            ,A.NAME
            ,A.DEPTH
    FROM    @tmp A
    WHERE   A.ParentID IS NULL

    UNION ALL

    SELECT   B.ID
            ,B.ParentID
            ,B.NAME
            ,B.DEPTH 
    FROM    cte A 
              INNER JOIN 
            @tmp B on B.ParentID = A.ID
    WHERE   B.ParentID IS NOT NULL

)

SELECT * FROM cte

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.