Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTE for parent child relation with multiple parent

I have a parent child relation table as shown below. I want to retrieve all records for a parent or child ID like all ancestors and parents and if possible with depth. For example I want to find the family of D, it will return the first 14 rows as all are of same family. There may be several set of such family. I want to query with one member and want to get whole family record. Is it possible to implement this using CTE? The family structures as per table record :

                      A
                     / \
                    B   C   G   J
                   /     \ / \ / \
              M   D       E   H   K
             / \ /             \ / \
            N   F               I   L


                 R
                 |
                 S   U
                  \ /
                   T

Please help. The table is like:

   Parent   Child
    ------  ------
    A            B
    A            C
    B            D
    D            F
    M            F
    M            N
    C            E
    G            E
    G            H
    J            H
    J            K
    H            I
    K            I
    K            L
    R            S
    S            T
    U            T

Thanks,

Himadri

like image 917
user3632629 Avatar asked May 13 '14 13:05

user3632629


People also ask

What is the use of CTE in database?

Here in this article I am taking an example where continents and their countries and state of that countries and cities of that states are stored in same table. And suppose it is required to get all of them in hierarchy e.g. Continent-> Country-> State-> City , then CTE is very useful in such case because of its recursive capability.

How do I combine two CTEs into one?

If you want all ancestors and all descendants, you can combine the two queries in one. Use the two CTEs and then a simple UNION: change the final UNION to UNION ALL by putting the starting node (s) in only one of the CTEs.

How do you get the CTE in recursive CTE query?

Recursive CTE query to get Continent-> Country-> State-> City relationship hierarchy with levels. Explanation: The base record for the CTE is obtained by the first select query above UNION ALL. It gets all the ParentIds which don’t have ParentId i.e. NULL value. This means they are the continents so their Level is set to 1.

How do I find the parent of a child in MyMy?

My parent-child tree structure will show data about parents and their children. Take a look: Here, the column id shows the child's ID. To find out who that child’s parent is, you have to look at the column parent_id, find the same ID number in the id column, and look in that row for the parent’s name.


1 Answers

I found a solution. But I used CTE in a while loop. If someone has any other solution please suggest. As I have mentioned a table above that contains family records or you can say graphs. Let's name it as tbl_ParentChild.

Here is my code:

Declare @Child varchar(10), @RowsEffected int
Set @Child='D'-----It is the member whose family we want to find

 CREATE Table #PrntChld (Parent varchar(10),Child varchar(10))
 Insert Into #PrntChld
 Select Parent,Child  from tbl_ParentChild MF
 Where MF.Child=@Child or MF.Parent=@Child

 Select @RowsEffected=Count(*) from #PrntChld

 While @RowsEffected>0 
 BEGIN
    ;WITH Prnt(Parent,Child)
        AS
        ( Select M.Parent,M.Child  from tbl_ParentChild M
            Inner Join #PrntChld F On F.Child=M.Child
          UNION ALL
          SELECT e.Parent,e.Child       
              FROM tbl_ParentChild AS E
              INNER JOIN  Prnt AS M                 
                  ON E.Child = M.Parent           
        ),
        PrntChld(Parent,Child)
        AS
        ( Select M.Parent,M.Child  from tbl_ParentChild M
            Inner Join (Select * from Prnt union Select * from #PrntChld) F On M.Parent=F.Parent
          UNION ALL
          SELECT e.Parent,e.Child       
              FROM tbl_ParentChild AS E
              INNER JOIN  PrntChld AS M                 
                  ON M.Child = E.Parent           
        )

    Insert Into #PrntChld
    Select distinct MF.* from PrntChld MF 
    Left Join #PrntChld T On T.Child =MF.Child and T.Parent  = MF.Parent    
    where T.Child is null
    Select @RowsEffected=@@ROWCOUNT

END

Select * from #PrntChld
drop table #PrntChld
like image 181
user3632629 Avatar answered Sep 18 '22 08:09

user3632629