根据您的评论,如果您知道只有两个级别,那么有一个简单的解决方案:
select *from @Features featorder by case when ParentID = 0 then Feature else ( select Feature from @Features parent where parent.FeatureID = feat.ParentID ) end, case when ParentID = 0 then 1 end desc, Feature
- 按根元素的名称排序:对于根,这是“功能”列。对于孩子,请使用子查询查找根的名称。
- 在根上排序根
- 按名称对孩子进行排序
SE Data的示例。



