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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. hierarchy

hierarchy

Scheduled Pinned Locked Moved Database
questiondatabase
3 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    mehrdadc48
    wrote on last edited by
    #1

    Hi, How can I find all leaves (nodes that don't have any child), in a hierarchy structure in my table in sql?

    Best wishes

    J I 2 Replies Last reply
    0
    • M mehrdadc48

      Hi, How can I find all leaves (nodes that don't have any child), in a hierarchy structure in my table in sql?

      Best wishes

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Peronally, when I represent Heirachies in a relational table I always add a boolean "IsLeaf" field so I can query just this situaltion easily (SELECT * FROM MyHeirachy WHERE IsLeaf=1) However, it should be easy enough without it, just look for any rows where there is no row referencing this row as its parent.

      SELECT *
      FROM MyHeirachy H
      WHERE NOT EXISTS (SELECT * FROM MyHeirachy WHERE ParentID=H.ID)

      1 Reply Last reply
      0
      • M mehrdadc48

        Hi, How can I find all leaves (nodes that don't have any child), in a hierarchy structure in my table in sql?

        Best wishes

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

        The following code uses the Employees table from Northwind. WITH EmployeeStructure AS ( SELECT employeeid, reportsto, 0 as level FROM Employees WHERE reportsto is null UNION ALL SELECT e.employeeid, e.reportsto, level + 1 FROM Employees e INNER JOIN EmployeeStructure es ON es.employeeid = e.reportsto ) SELECT * FROM EmployeeStructure WHERE EmployeeID NOT IN (SELECT reportsto FROM EmployeeStructure GROUP BY reportsto HAVING ReportsTo IS NOT NULL) ORDER BY level ASC

        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