Get last child from sql hierarchyid

坚强是说给别人听的谎言 提交于 2019-12-11 21:09:48

问题


I have hierarchyid in my tables like this.

/1/ - This is Category 
/1/1/ - This is SubCategory
/1/1/1/ - This is Item
/1/1/2/ - Item
/1/1/3/ - Item
/1/2/ - SubCategory
/1/2/1/ - Item
/1/2/2/ - Item
/1/2/3 - Item

I want to get the last child regardless of Category, Subcategory or Items using SQL Query. For Eg. If its category, It will return /1/ ; If It's Subcategory It should return /1/ or /2/ (second child/last child); If it's Item it should return 3rd child/last child.

I tried this query. But, it's returning me first part only.

SELECT PricedItemHID.GetAncestor(PricedItemHID.GetLevel() -1) FROM ItemsList

回答1:


You can use something like this :

Select @MaxChild = Max(OrgNode) from HumanResources.EmployeeDemo

this link can help you [http://blogs.msdn.com/b/manisblog/archive/2007/08/28/sql-server-2008-hierarchyid-part-ii.aspx][1]



来源:https://stackoverflow.com/questions/30963130/get-last-child-from-sql-hierarchyid

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