Get Child Record under Parent record with SQL Query without loops using

  • Table: tbUser

    ID Name Descr ParentName

    1 XYZ this is Parent record NULL

    2 ABC this is parent record NULL

    3 PQR this is child record 1

    4 STU this is Child Record 1

    5 MNO this is child 1

    6 IJK this is child 2

    Expected Result Like this:

    ID Parent Name

    1 Select XYZ

    1 PQR

    1 STU

    1 MNO

    2 Select ABC

    2 IJK

  • select id, [Name], Descr, isnull(parentId, id) as ParentId2 from #tbUser

    order by ParentId2, id 😛

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

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