Find all leaf node records using hierarchyid

送分小仙女□ 提交于 2019-12-07 17:20:44

问题


How would I go about querying for only records that are "only leaf nodes" (ie. no children)?

I have tried a query like this:

select *
from TableA tt
where tt.HierarchyId.GetDescendant(null, null) not in 
(
    Select  t.HierarchyId
    from TableA t
)

But this still seemed to return some nodes that had children.

I am using the built-in hierarchyid data type (part of ms sqlserver)


回答1:


SELECT A.HieracrchyId, A.HierarchyId.ToString()
  FROM dbo.TableA AS A 
  LEFT OUTER JOIN dbo.TableA AS B
  ON A.HierarchyId = B.HierarchyId.GetAncestor(1)
  WHERE B.HierarchyId IS NULL;


来源:https://stackoverflow.com/questions/10694894/find-all-leaf-node-records-using-hierarchyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!