问题
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