return parent child relation

  • PRNT CHLD POS

    1050000 1000000 1

    1050000 5000000 2

    1050001 1000004 1

    1050001 5000002 2

    ..............................................

    ..............................................

    1000000 1010000 1

    1000000 1010001 2

    1000000 3030000 3

    ..............................................

    ..............................................

    1010000 2020000 1

    1010001 2030000 1

    ....................................... ......

    .............................................

    IN THE ABOVE TABLE EACH PARENT HAS A CHILD MAY BE MORE THAN ONE. AGAIN CHILD BECOME PARENT NOT ALL THE CHILD AND IT WILL GOS ON UPTO SOME LEVEL THEN CHILD WONT BE A PARENT.

    CURRENTLY I AM USING BELOW QUERY

    select * from TABLE_NAME where

    PRNT

    in

    ('1050000',

    '1000000',

    '1010000',

    '1010001')

    order by case

    PRNT

    when '1050000' then 1

    when '1000000' then 2

    when '1010000' then 3

    when '1010001' then 4

    end , POS ASC

    AND RESULT WILL BE

    PRNT CHLD POS

    1050000 1000000 1

    1050000 5000000 2

    1000000 1010000 1

    1000000 1010001 2

    1000000 3030000 3

    1010000 2020000 1

    1010001 2030000 1

    IS THERE ANY OTHER WAY TO GET THE ABOVE RESULTS USING THE 1ST PARENT CODE(1050000) TO RETURN UP TO THE LAST CHILD(2030000)

  • I believe a recursive cte is what you are looking for. If you want some more specific help you need to post some details. Take a look at the first link in my signature about best practices for how to post questions in such a way that it will generate a response.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Agree with Sean,

    Recursive CTE like below,

    [Code]

    WITH Tree AS

    (

    SELECT Parent.ID[ID]

    , Parent.ParentID[ParentID]

    FROM Entity Parent

    WHERE Parent.ID = @Parammeter

    UNION ALL

    SELECT Child.ID

    , Child.ParentID

    FROM Entity Child

    INNER JOIN TreeON Child.ParentID = Tree.ID

    )

    SELECT *

    FROM Tree

    [/Code]

  • Dear Sean Lange & SSC Rookie,

    Thanks for your reply.I cant able to show my table in this forum. Can you tell me how to add image in this forum then i will explain clearly that what i am really struggling.

  • vishnurajeshsat (8/2/2012)


    Dear Sean Lange & SSC Rookie,

    Thanks for your reply.I cant able to show my table in this forum. Can you tell me how to add image in this forum then i will explain clearly that what i am really struggling.

    Why can't you share the table structure? If it is because of not wanting to share your real structure just make something up that is close and only has the columns you need. Change the names and such to protect yourself. Then post some sample data. Again change values and do what you need to protect the real data. Everything you can do to make something close to your real issue will help.

    An image is not really going to provide much to allow us to help you with the code.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • You posted a bunch of stuff in your first post that somewhat looks like it came from your table. Turn that into something we can use and we can help.

    Turn that stuff into a create table statement and some insert statements for the data.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • an example of what Sean is asking you for: a CTE or CREATE TABLE with his first post:

    With MyCTE (PRNT,CHLD,POS)

    AS

    (

    SELECT '1050000','1000000','1' UNION ALL

    SELECT '1050000','5000000','2' UNION ALL

    SELECT '1050001','1000004','1' UNION ALL

    SELECT '1050001','5000002','2' UNION ALL

    SELECT '1000000','1010000','1' UNION ALL

    SELECT '1000000','1010001','2' UNION ALL

    SELECT '1000000','3030000','3' UNION ALL

    SELECT '1010000','2020000','1' UNION ALL

    SELECT '1010001','2030000','1'

    )

    select * from MyCTE where

    PRNT

    in

    ('1050000',

    '1000000',

    '1010000',

    '1010001')

    order by case

    PRNT

    when '1050000' then 1

    when '1000000' then 2

    when '1010000' then 3

    when '1010001' then 4

    end , POS ASC

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Here is All you want, with dummy data,

    CREATE TABLE #Test

    (

    IDINT,

    ParentIDINT,

    TNameVARCHAR(25)

    )

    INSERT INTO #Test

    (

    ID,

    ParentID,

    TName

    )

    SELECT 1,NULL,'Granfather'

    UNION ALL

    SELECT 2,1,'father1'

    UNION ALL

    SELECT 3,1,'father2'

    UNION ALL

    SELECT 4,2,'1st-son-father1'

    UNION ALL

    SELECT 5,2,'2nd-son-father1'

    UNION ALL

    SELECT 6,3,'1st-son-father2'

    UNION ALL

    SELECT 7,3,'2nd-son-father2'

    SELECT * FROM #Test

    --------------------------------------------

    -- Recursive CTE

    ;WITH Tree AS

    (

    SELECT Parent.ID[ID],

    Parent.ParentID[ParentID],

    Parent.TName

    FROM #Test Parent

    WHERE Parent.ID =2--@ID -- Pass the Top Id you want to see children of it.

    UNION ALL

    SELECT Child.ID,

    Child.ParentID,

    Child.TName

    FROM #Test Child

    INNER JOIN TreeON Child.ParentID = Tree.ID

    )

    SELECT *

    FROM Tree

    DROP TABLE #Test

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply