Query

  • I have a data as

    ParentID ChildID

    1 2

    1 3

    1 4

    2 5

    3 6

    Please let me know the query which should give me data as

    1 -- ParenID

    2 --ChildID

    1 --ParentID

    2 --ChildID

    1

    4 "

    2

    5 "

    3

    6

    Please give me solution for this.Thanks in advance.

  • try this code..

    SELECT ParentId , '--Parentid' AS TypeId

    FROM YouTable

    UNION

    SELECT ChildId , '--Childid' AS TypeId

    FROM YourTable

  • steveb (3/4/2009)


    try this code..

    SELECT ParentId , '--Parentid' AS TypeId

    FROM YouTable

    UNION

    SELECT ChildId , '--Childid' AS TypeId

    FROM YourTable

    Instead of UNION use UNION ALL because the UNION statement select only distinct values the UNION ALL select all values!

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Dugi (3/4/2009)


    steveb (3/4/2009)


    try this code..

    SELECT ParentId , '--Parentid' AS TypeId

    FROM YouTable

    UNION ALL

    SELECT ChildId , '--Childid' AS TypeId

    FROM YourTable

    Instead of UNION use UNION ALL because the UNION statement select only distinct values the UNION ALL select all values!

    good point !! thanks

  • steveb (3/4/2009)


    good point !! thanks

    As always all helps and alternative suggests are welcomed from each-other!

    😉

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • I am not getting the result as I want

    MY data in the table is as

    Node Child_Node

    41454285

    41454286

    41454287

    42454285

    42454286

    42454287

    by the solution provided by you i am getting result as:-

    Node TypeID

    41--Parentid

    41--Parentid

    41--Parentid

    42--Parentid

    42--Parentid

    42--Parentid

    454285--Childid

    454286--Childid

    454287--Childid

    454285--Childid

    454286--Childid

    454287--Childid

    But I want result as Child should be shown immediate after the parent as shown below.

    Node TypeID

    41--Parentid

    454285--Childid

    41--Parentid

    454286--Childid

    41--Parentid

    454287--Childid

    42--Parentid

    454285--Childid

    42--Parentid

    454286--Childid

    42--Parentid

    454287--Childid

    OR result can be like this also.

    Node TypeID

    41--Parentid

    454285--Childid

    454286--Childid

    454287--Childid

    42--Parentid

    454285--Childid

    454286--Childid

    454287--Childid

    Please provide solution for this.Thanks.

  • Thanks a lot this is working fine. Feeling great to use the this portal for queries.:)

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

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