Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Parent-children Listing

Parent-children Listing

Scheduled Pinned Locked Moved Database
help
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Ali Al Omairi Abu AlHassan

    guys i have a table with a self-referencing relationship. I want to get the child records listed just below their parent. just like: id parent value --- ------ ------ 1 null node 1 4 1 node 1/1 6 4 node 1/1/1 5 1 node 1/2 7 1 node 1/3 2 null node 2 3 null node 3 8 3 node 3/1 any help;

    Help people,so poeple can help you.

    C Offline
    C Offline
    Chris Meech
    wrote on last edited by
    #2

    Include the parent id and the child id in each record and then include them as the first two fields to sort by. :)

    select p.id
    ,c.id
    , .....
    from some_table p
    ,some_table c
    where p.id = c.parent_id_field
    sort by p.id, c.id

    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

    A 1 Reply Last reply
    0
    • C Chris Meech

      Include the parent id and the child id in each record and then include them as the first two fields to sort by. :)

      select p.id
      ,c.id
      , .....
      from some_table p
      ,some_table c
      where p.id = c.parent_id_field
      sort by p.id, c.id

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      A Offline
      A Offline
      Ali Al Omairi Abu AlHassan
      wrote on last edited by
      #3

      but sir, what about multi-level threads?

      Help people,so poeple can help you.

      C 1 Reply Last reply
      0
      • A Ali Al Omairi Abu AlHassan

        but sir, what about multi-level threads?

        Help people,so poeple can help you.

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #4

        You will need to know how many levels deep you want to go and join the table for each level. You might also be able to use a view to help with that. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        A 1 Reply Last reply
        0
        • C Chris Meech

          You will need to know how many levels deep you want to go and join the table for each level. You might also be able to use a view to help with that. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          A Offline
          A Offline
          Ali Al Omairi Abu AlHassan
          wrote on last edited by
          #5

          i can't tie a user hands. is there "Recursion" in sql server.

          Help people,so poeple can help you.

          C 1 Reply Last reply
          0
          • A Ali Al Omairi Abu AlHassan

            guys i have a table with a self-referencing relationship. I want to get the child records listed just below their parent. just like: id parent value --- ------ ------ 1 null node 1 4 1 node 1/1 6 4 node 1/1/1 5 1 node 1/2 7 1 node 1/3 2 null node 2 3 null node 3 8 3 node 3/1 any help;

            Help people,so poeple can help you.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #6

            Hi, You could use a recursive query for this. CTE (Common Table Expression) has the capability of handling unkown amount of recursions. For an example, seefor example http://msdn.microsoft.com/en-us/library/ms186243.aspx[^]. Best regards, mika

            The need to optimize rises from a bad design.My articles[^]

            A 1 Reply Last reply
            0
            • W Wendelius

              Hi, You could use a recursive query for this. CTE (Common Table Expression) has the capability of handling unkown amount of recursions. For an example, seefor example http://msdn.microsoft.com/en-us/library/ms186243.aspx[^]. Best regards, mika

              The need to optimize rises from a bad design.My articles[^]

              A Offline
              A Offline
              Ali Al Omairi Abu AlHassan
              wrote on last edited by
              #7

              Over my head, mika; you are still the best. 100 :rose:

              Help people,so poeple can help you.

              W I 2 Replies Last reply
              0
              • A Ali Al Omairi Abu AlHassan

                i can't tie a user hands. is there "Recursion" in sql server.

                Help people,so poeple can help you.

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #8

                Ali Al Omairi(Abu AlHassan) wrote:

                i can't tie a user hands.

                Sometimes, I wish I could do that. :-D

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                1 Reply Last reply
                0
                • A Ali Al Omairi Abu AlHassan

                  Over my head, mika; you are still the best. 100 :rose:

                  Help people,so poeple can help you.

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #9

                  Thanks, but frankly I believe that this site has a bunch of real experts thus making CP so special. No other site I've come across has so positive and professional attitude at the same time as this.

                  The need to optimize rises from a bad design.My articles[^]

                  1 Reply Last reply
                  0
                  • A Ali Al Omairi Abu AlHassan

                    Over my head, mika; you are still the best. 100 :rose:

                    Help people,so poeple can help you.

                    I Offline
                    I Offline
                    i j russell
                    wrote on last edited by
                    #10

                    CREATE TABLE #data
                    (
                    ID INT NOT NULL,
                    PID INT NULL,
                    VALUE VARCHAR(20) NOT NULL
                    );

                    INSERT INTO #data
                    (ID, PID, VALUE)
                    VALUES
                    (1, NULL, 'node 1'),
                    (4, 1, 'node 1/1'),
                    (6, 4, 'node 1/1/1'),
                    (5, 1, 'node 1/2'),
                    (7, 1, 'node 1/3'),
                    (2, NULL, 'node 2'),
                    (3, NULL, 'node 3'),
                    (8, 3, 'node 3/1');

                    WITH MyCTE AS
                    (
                    SELECT ID, PID, VALUE, 0 AS [LEVEL]
                    FROM #data
                    WHERE PID IS NULL
                    UNION ALL
                    SELECT D.ID, D.PID, D.VALUE, [LEVEL] + 1
                    FROM MyCTE M
                    JOIN #data D
                    ON D.PID = M.ID
                    )
                    SELECT ID, PID, VALUE, [LEVEL]
                    FROM MyCTE;

                    DROP TABLE #data;

                    A 1 Reply Last reply
                    0
                    • I i j russell

                      CREATE TABLE #data
                      (
                      ID INT NOT NULL,
                      PID INT NULL,
                      VALUE VARCHAR(20) NOT NULL
                      );

                      INSERT INTO #data
                      (ID, PID, VALUE)
                      VALUES
                      (1, NULL, 'node 1'),
                      (4, 1, 'node 1/1'),
                      (6, 4, 'node 1/1/1'),
                      (5, 1, 'node 1/2'),
                      (7, 1, 'node 1/3'),
                      (2, NULL, 'node 2'),
                      (3, NULL, 'node 3'),
                      (8, 3, 'node 3/1');

                      WITH MyCTE AS
                      (
                      SELECT ID, PID, VALUE, 0 AS [LEVEL]
                      FROM #data
                      WHERE PID IS NULL
                      UNION ALL
                      SELECT D.ID, D.PID, D.VALUE, [LEVEL] + 1
                      FROM MyCTE M
                      JOIN #data D
                      ON D.PID = M.ID
                      )
                      SELECT ID, PID, VALUE, [LEVEL]
                      FROM MyCTE;

                      DROP TABLE #data;

                      A Offline
                      A Offline
                      Ali AlOmairi TJIC
                      wrote on last edited by
                      #11

                      thank you Russell. ;) 100 :rose:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups